Apache下的Rewrite規則是詳細的規則,Nginx下的中文文檔好像沒有看到,這是英文的文檔:http://wiki.nginx.org/NginxHttpRewriteModule
Apache下的Rewrite規則基本上到nginx下,也可以直接使用,不行的話用引號引起來一般就都可以了。下面是我收集整理的一些記錄。
nginx的rewrite格式是:rewrite regex replacement flag
其中flag標記有四種格式:
last – 相當于Apache中的L
break – 中止Rewirte,不在繼續匹配
redirect – 返回臨時重定向的HTTP狀態302,相當于Apache中的R
permanent – 返回永久重定向的HTTP狀態301,相當于Apache中的R=301
可以放在server, location 和 if 模塊中。
匹配判斷
~ 為區分大小寫匹配; !~為區分大小寫不匹配
~* 為不區分大小寫匹配;!~為不區分大小寫不匹配
例如下面設定nginx在用戶使用ie的使用重定向到/nginx-ie目錄下:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
一些常用的nginx下的Rewrite代碼。
1.只使用一個網址,比如主力網址設為www.pc004.com。
if ($host != 'www.pc004.com' ) { rewrite ^/(.*)$ http://www.pc004.com/$1 permanent; } |
訪問pc004.com時,會自動跳轉到www.pc004.com。
2.防盜鏈
location ~* .(gif|jpg|png|swf|flv)$ { valid_referers none blocked 9enjoy.com itlearner.com; if ($invalid_referer) { return 403; } } |
盜鏈時則返回403錯誤,允許的域名可以直接跟在第二行的域名后面。
3.wordPress/ target=_blank class=infotextkey>WordPress的Rewrite
location / { index index.html index.php; if (-f $request_filename/index.html){ rewrite (.*) $1/index.html break; } if (-f $request_filename/index.php){ rewrite (.*) $1/index.php; } if (!-f $request_filename){ rewrite (.*) /index.php; } } |
目前,代碼收藏上就是使用的這段代碼。
4.bo-blog在nginx下的Rewrite規則
if (!-e $request_filename) { rewrite ^/post/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ /read.php?entryid=$1&page=$2&part=$3 last; rewrite ^/page/([0-9]+)/([0-9]+)/?$ /index.php?mode=$1&page=$2 last; rewrite ^/starred/([0-9]+)/?([0-9]+)?/?$ /star.php?mode=$1&page=$2 last; rewrite ^/category/([^/]+)/?([0-9]+)?/?([0-9]+)?/?$ /index.php?go=category_$1&mode=$2&page=$3 last; rewrite ^/archiver/([0-9]+)/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ /index.php?go=archive&cm=$1&cy=$2&mode=$3&page=$4 last; rewrite ^/date/([0-9]+)/([0-9]+)/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ /index.php?go=showday_$1-$2-$3&mode=$4&page=$5 last; rewrite ^/user/([0-9]+)/?$ /view.php?go=user_$1 last; rewrite ^/tags/([^/]+)/?([0-9]+)?/?([0-9]+)?/?$ /tag.php?tag=$1&mode=$2&page=$3 last; rewrite ^/component/id/([0-9]+)/?$ /page.php?pageid=$1 last; rewrite ^/component/([^/]+)/?$ /page.php?pagealias=$1 last; #Force redirection for old rules rewrite ^/read.php/([0-9]+).htm$ http://$host/post/$1/ permanent; rewrite ^/post/([0-9]+).htm$ http://$host/post/$1/ permanent; rewrite ^/post/([0-9]+)_([0-9]+).htm$ http://$host/post/$1/$2/ permanent; rewrite ^/post/([0-9]+)_([0-9]+)_([0-9]+).htm$ http://$host/post/$1/$2/$3/ permanent; rewrite ^/index_([0-9]+)_([0-9]+).htm$ http://$host/page/$1/$2/ permanent; rewrite ^/star_([0-9]+)_([0-9]+).htm$ http://$host/starred/$1/$2/ permanent; rewrite ^/category_([0-9]+).htm$ http://$host/category/$1/ permanent; rewrite ^/category_([0-9]+)_([0-9]+)_([0-9]+).htm$ http://$host/category/$1/$2/$3/ permanent; rewrite ^/archive_([0-9]+)_([0-9]+).htm$ http://$host/archiver/$1/$2/ permanent; rewrite ^/archive_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+).htm$ http://$host/archiver/$1/$2/$3/$4/ permanent; rewrite ^/showday_([0-9]+)_([0-9]+)_([0-9]+).htm$ http://$host/date/$1/$2/$3/ permanent; rewrite ^/showday_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+).htm$ http://$host/date/$1/$2/$3/$4/$5/ permanent; #Filename alias rewrite ^/([a-zA-Z0-9_-]+)/?([0-9]+)?/?([0-9]+)?/?$ /read.php?blogalias=$1&page=$2&part=$3 last; } |