常见开源PHP平台在Nginx下的Rewrite规则

时间: 2009-06-08 / 分类: 技术相关 / 浏览次数: 3,163 views / 9个评论 发表评论

[文章作者:叶歆昊   最后修改:2010-07-19   转载请注明原文链接:http://littz.com/common-open-source-php-platform-under-nginx-rewrite-rules.html]

首先解释Nginx的Rewrite规则:

Rewrite其实就是跳转(URL重定向),规则为:详细规则请查阅Nginx官方维基http://wiki.nginx.org/NginxHttpRewriteModule

rewrite regex replacement flag;

#rewrite是关键词,表示做跳转,regex为正则表达式的判断,replacement为跳转(重定向)目的地,flag分为以下几种:

last – 基本上都用这个Flag(也是默认值),重定向之后浏览器上显示的该网页URL不会更改。

break – 中止Rewirte,不在继续匹配

redirect – 返回临时重定向的HTTP状态302,重定向之后浏览器上显示的该网页URL会更改。

permanent – 返回永久重定向的HTTP状态301,重定向之后浏览器上显示的该网页URL会更改。

关于regex的正则表达式请查阅相关资料,例http://zh.wikipedia.org/wiki/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F简单说 ^表示以某个字符串开始,$表示以某个字符串为结束,[a-zA-Z0-9]表示匹配集内的字符,a+表示至少含有一个a字符,[0-9]+表示含有至少一个0~9集合内的字符,(.+)表任意。可参看下列举例自行推测

举例:(请将该Rewrite规则写于Nginx的配置文件server段或者location段中)

rewrite ^/about$ /action-channel-name-about.html permanent;
#匹配URL请求只为/about,并301跳转到/action-channel-name-about.html
rewrite ^/album http://picasaweb.google.com/littlehz  permanent; #匹配URL请求为/album开始,后面可含有任意字符串,302跳转到http://picasaweb.google.com/littlehz页面

网友可自行分析本人在此页面书写的一些rewrite含义http://littz.com/viewnews-342.html

下面列举一些常见的PHP平台的rewrite规则,首先请在该PHP程序中设置永久链接URL为rewrite模式。

###以下为Wordpress 2.7的Nginx伪静态化rewrite规则
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;
        }
#这里做了几个判断,先判断请求的文件是否存在,如果不存在,才把任意字符rewrite至wordpress的逻辑index.php处理

###以下为安装有WP Super Cache插件的Wordpress的Nginx伪静态化rewrite规则
#Super Cache插件是Wordpress的缓存插件,这里的rewrite就是主要判断缓存是否存在,如果存在,跳转缓存文件,如果没有缓存,才让wordpress 的index.php来做URL处理。

if (-f $request_filename) { break;}
set $supercache_file '';
set $supercache_uri $request_uri;
if ($request_method = POST) {set $supercache_uri '';}
if ($query_string) {set $supercache_uri '';}
if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) { set $supercache_uri ''; }
if ($supercache_uri ~ ^(.+)$) { set $supercache_file /wp-content/cache/supercache/$http_host/$1index.html; }
if (-f $document_root$supercache_file) { rewrite ^(.*)$ $supercache_file break; }
if (!-e $request_filename) { rewrite . /index.php last;}

###以下为Discuz! 7.0 的Nginx伪静态化rewrite规则
rewrite ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2 last;
rewrite ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3 last;
rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^(.*)/profile-(username|uid)-(.+)\.html$ $1/viewpro.php?$2=$3 last;
rewrite ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3 last;
rewrite ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2 last;

###以下为SupeSite 7.0 的Nginx伪静态化rewrite规则
rewrite ^/([0-9]+)/spacelist(.+)$ /index.php?uid/$1/action/spacelist/type$2 last;
rewrite ^/([0-9]+)/viewspace(.+)$ /index.php?uid/$1/action/viewspace/itemid$2 last;
rewrite ^/([0-9]+)/viewbbs(.+)$ /index.php?uid/$1/action/viewbbs/tid$2 last;
rewrite ^/([0-9]+)/(.*)$ /index.php?uid/$1/$2 last;
rewrite ^/([0-9]+)$ /index.php?uid/$1 last;
rewrite ^/action(.+)$ /index.php?action$1 last;
rewrite ^/category(.+)$ /index.php?action/category/catid$1 last;
rewrite ^/viewnews(.+)$ /index.php?action/viewnews/itemid$1 last;
rewrite ^/viewthread(.+)$ /index.php?action/viewthread/tid$1 last;
rewrite ^/mygroup(.+)$ /index.php?action/mygroup/gid$1 last;

###以下为PHPCMS 2008 的Nginx伪静态化rewrite规则
rewrite ^(.*)show-([0-9]+)-([0-9]+)\.html$ $1/show.php?itemid=$2&page=$3;
rewrite ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1/list.php?catid=$2&page=$3;
rewrite ^(.*)show-([0-9]+)\.html$ $1/show.php?specialid=$2;

####以下为PHPWind 7.3的Nginx伪静态化rewrite规则
rewrite ^(.*)-htm-(.*)$ $1.php?$2 last;
rewrite ^(.*)/simple/([a-z0-9\_]+\.html)$ $1/simple/index.php?$2 last;

9个评论

  1. Guest
    2009/07/18 于 07:13:18

    老叶 咱们找几个弄个论坛如何 主要弄弄非专业化的LEMP(NGINX) 呀VPS优化什么的 如何
    BY LAND

  2. littlehz
    2009/07/19 于 08:15:14

    访问这个页面链接被重置,弄得我用https的才显示,Fuck G/F.w!论坛的事可以考虑你主建一个,我一有空就去逛逛、维护。我现在已经是Discuz!官方论坛实习版主,事情比较多,下学期一开学就找工作去,会很忙,没很多精力弄。

  3. Guest
    2009/07/20 于 13:37:58

    连接重置?FUCK 不是我开的是韩乾!
    BY LAND

    • littlehz
      2009/07/20 于 14:12:47

      我知道Fuck是hanqian开的,那天一进这个文章页面连接就被重置,很气愤,Fuck做动词用。还好我搭建了https!~

      • Guest
        2009/07/20 于 19:19:41

        debug 结果
        Must remake target `ext/date/lib/parse_date.lo’.
        /bin/sh /data0/software/php-5.2.10/libtool –silent –preserve-dup-deps –mode=compile gcc -Iext/date/lib -Iext/date/ -I/data0/software/php-5.2.10/ext/date/ -DPHP_ATOM_INC -I/data0/software/php-5.2.10/include -I/data0/software/php-5.2.10/main -I/data0/software/php-5.2.10 -I/data0/software/php-5.2.10/ext/date/lib -I/usr/include/libxml2 -I/usr/kerberos/include -I/usr/include/freetype2 -I/data0/software/php-5.2.10/ext/mbstring/oniguruma -I/data0/software/php-5.2.10/ext/mbstring/libmbfl -I/data0/software/php-5.2.10/ext/mbstring/libmbfl/mbfl -I/usr/local/include -I/usr/local/webserver/mysql/include/mysql -I/data0/software/php-5.2.10/TSRM -I/data0/software/php-5.2.10/Zend -I/usr/local/include -g -O2 -c /data0/software/php-5.2.10/ext/date/lib/parse_date.c -o ext/date/lib/parse_date.lo
        Putting child 0x006f51e0 (ext/date/lib/parse_date.lo) PID 17992 on the chain.
        Live child 0x006f51e0 (ext/date/lib/parse_date.lo) PID 17992
        Live child 0x006f51e0 (ext/date/lib/parse_date.lo) PID 17992

      • Guest
        2009/07/20 于 18:46:57

        我 弄vps了 但是 按php 不论5。2。8
        5。2。10都卡/bin/sh /data0/software/php-5.2.8/libtool –silent –preserve-dup-deps –mode=compile gcc -Iext/date/lib -Iext/date/ -I/data0/software/php-5.2.8/ext/date/ -DPHP_ATOM_INC -I/data0/software/php-5.2.8/include -I/data0/software/php-5.2.8/main -I/data0/software/php-5.2.8 -I/usr/include/libxml2 -I/usr/kerberos/include -I/data0/software/php-5.2.8/ext/date/lib -I/usr/include/freetype2 -I/data0/software/php-5.2.8/ext/mbstring/oniguruma -I/data0/software/php-5.2.8/ext/mbstring/libmbfl -I/data0/software/php-5.2.8/ext/mbstring/libmbfl/mbfl -I/usr/local/include -I/usr/local/webserver/mysql/include/mysql -I/data0/software/php-5.2.8/TSRM -I/data0/software/php-5.2.8/Zend -I/usr/local/include -g -O2 -c /data0/software/php-5.2.8/ext/date/lib/parse_date.c -o ext/date/lib/parse_date.lo 一直卡住了

  4. 中国第一花木网
    2011/07/03 于 12:39:38

    rewrite ^/list-c-([0-9]+)-k-(.+)-o-1\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=1 last;
    rewrite ^/list-c-([0-9]+)-k-(.+)-o-1-p-(.+)\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=1&p=$3 last;
    rewrite ^/list-c-([0-9]+)-k-(.+)-o-1-price-(.+)\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=1&price=$3 last;
    rewrite ^/list-c-([0-9]+)-k-(.+)-o-(.+)-p-1\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=$3&p=1 last;
    rewrite ^/list-c-([0-9]+)-k-(.+)-o-(.+)-p-(.+)\.html$ /index.php?module=Default&action=List&c=$1&k=$2&o=$3&p=$4 last;
    这几条放一起就不行了 怎么解决啊

  5. jj
    2011/09/21 于 10:46:03

    rewrite规则中的$1 这个代码的具体含义是什么呀??

  6. 姣姣
    2011/09/21 于 10:48:39

    在rewrite规则中的$1 这个该怎么解释呢?

发表评论

您的昵称 *

您的邮箱 * (绝对保密)

您的网站