rewrite 重寫規則可以讓網站URL看起來非常簡介,對搜索引擎友好,有利于給網站用戶帶來記憶上的方便,給網站帶來更多流量。
因此編寫網站rewrite規則很重要。Nginx的rewrite規則非常強大,下面是樓主實際使用過的一些配置最為例子放出來給大家參考。
注意寫到location里面,否則$request_filename找不對路徑,無效。
location / {
root /home/freeworm/www/nginx.com.cn;
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^/pic/(.*)/.png siteimage.php?q=$1 last;
rewrite ^/pic/(.*) /siteimage.php?q=$1 last;
rewrite ^/([^/]*)/(.*)$ /main.php?q=$1&force=$2 last;
rewrite ^/(.*)$ /main.php?q=$1 last;
rewrite ^/$ /main.php last;
break;
}
}
if (!-f $request_filename) {
rewrite ^/[^./]*_([0-9]+)/.html$ /index.php?bookid=$1;
rewrite ^/[^./]*_([0-9]+)$ /index.php?cat=$1;
rewrite ^/[^./]*tag([0-9]+)$ /index.php?tag=$1;
rewrite ^/[^./]*-([0-9]+)-([0-9]+)$ /index.php?cat=$1&page=$2;
rewrite sitemap.xml /sitemap.php;
break;
}
if (!-f $request_filename) {
rewrite ^/pxy_(.*) /proxy/index.php?q=$1;
}
下面是應用于Discuz論壇的Nginx rewrite規則:
rewrite ^/archiver/((fid|tid)-[/w/-]+/.html)$ archiver/index.php?$1;
rewrite ^/forum-([0-9]+)-([0-9]+)/.html$ forumdisplay.php?fid=$1&page=$2;
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)/.html$ viewthread.php?tid=$1&extra=page/%3D$3&page=$2;
rewrite ^/space-(username|uid)-(.+)/.html$ space.php?$1=$2;
rewrite ^/tag-(.+)/.html$ tag.php?name=$1;
rewrite ^/forum-([0-9]+)-([0-9]+)/.html$ /forumdisplay.php?fid=$1&page=$2 last;
新聞熱點
疑難解答