Nginx 一些常用的URL 重寫(xiě)方法
1. 在 Apache 的寫(xiě)法
RewriteCond %{HTTP_HOST} nginx.org
RewriteRule (.*) http://www.nginx.org$1
在 Nginx 可以對(duì)應(yīng)寫(xiě)成:
server {
listen 80;
server_name www.nginx.org nginx.org;
if ($http_host = nginx.org) {
rewrite (.*) http://www.nginx.org$1;
}
...
}
但 Nginx 作者更建議的方法是:
server {
listen 80;
server_name nginx.org;
rewrite ^ http://www.nginx.org$request_uri?;
}
server {
listen 80;
server_name www.nginx.org;
...
}
F&Q:
1. 請(qǐng)教一下,nginx的rewrite規(guī)則怎么寫(xiě)?
比如將 http://http://www.oschina.net/222.html rewrite為 http://http://www.oschina.net/222.htm
location ~ .*\.(html)$
{
rewrite ^(.*)\.html $1.htm permanent;
}
2. 下面url要怎么寫(xiě)rewrite?
www.aaa.com/search/?wd=搜索內(nèi)容 ==> www.aaa.com/searchpage?keyword=搜索內(nèi)容
location ~ ^/search/ {
rewrite (.*) /searchpage$1 ;
}
3. 請(qǐng)求的url如下 /item/12345/index.html 重定向到/item/12/12345/index.html
規(guī)則就是id除1000,如果小于id小于1000,則為/item/0/id/index.html
不知道說(shuō)清楚沒(méi)有,這個(gè)rewrite規(guī)則該怎么寫(xiě)啊?
剛看了文檔,似乎可以
http://wiki.nginx.org/HttpRewriteModule
/photos/123456 -> /path/to/photos/12/1234/123456.png
rewrite "/photos/([0-9] {2})([0-9] {2})([0-9] {2})" /path/to/photos/$1/$1$2/$1$2$3.png;
備注:
有一個(gè)工具是apache htaccess 文件轉(zhuǎn) nginx rewrite:
http://www.anilcetin.com/convert-apache-htaccess-to-nginx/