首页 > 开发 > Nginx > 正文

Nginx rewrite重写规则实例和注意事项

2020-02-10 00:18:24
字体:
来源:转载
供稿:网友

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;

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表