Nginx 一些常用的URL 重寫方法
1. 在 Apache 的寫法
RewriteCond %{HTTP_HOST} nginx.org
RewriteRule (.*) http://www.nginx.org$1
在 Nginx 可以對應寫成:
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. 請教一下,nginx的rewrite規則怎么寫?
比如將 http://http://www.oschina.net/222.html rewrite為 http://http://www.oschina.net/222.htm
location ~ .*\.(html)$
{
rewrite ^(.*)\.html $1.htm permanent;
}
2. 下面url要怎么寫rewrite?
www.aaa.com/search/?wd=搜索內容 ==> www.aaa.com/searchpage?keyword=搜索內容
location ~ ^/search/ {
rewrite (.*) /searchpage$1 ;
}
3. 請求的url如下 /item/12345/index.html 重定向到/item/12/12345/index.html
規則就是id除1000,如果小于id小于1000,則為/item/0/id/index.html
不知道說清楚沒有,這個rewrite規則該怎么寫啊?
剛看了文檔,似乎可以
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;
備注:
有一個工具是apache htaccess 文件轉 nginx rewrite:
http://www.anilcetin.com/convert-apache-htaccess-to-nginx/