rewrite

Discuz! X1在nginx下的rewrite规则

        location / {
            root   /home/www/typengine;
            index  index.php index.html index.htm;
rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
rewrite ^([^\.]*)/article-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2 last;
rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;

Discuz! 7.2在Apache下的伪静态规则

DirectoryIndex index.php index.html index.htm  pagename.html
RewriteEngine On
RewriteBase /
RewriteRule ^archiver/((fid|tid)-[\w\-]+\.html)$ archiver/index.php?$1
RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2
RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ viewthread.php?tid=$1&extra=page\%3D$3&page=$2
RewriteRule ^space-(username|uid)-(.+)\.html$ space.php?$1=$2
RewriteRule ^tag-(.+)\.html$ tag.php?name=$1
RewriteRule ^index\.html index.php

nginx rewrite 参数和例子

Mailing list ARChives http://marc.info/?l=nginx
正则表达式匹配,其中:
* ~ 为区分大小写匹配
* ~* 为不区分大小写匹配
* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配
文件及目录匹配,其中:
* -f和!-f用来判断是否存在文件
* -d和!-d用来判断是否存在目录
* -e和!-e用来判断是否存在文件或目录
* -x和!-x用来判断文件是否可执行
flag标记有:
* last 相当于Apache里的[L]标记,表示完成rewrite
* break 终止匹配, 不再匹配后面的规则

常见PHP程序在Nginx下的rewrite规则

PHPCMS,shopex,ECSHOP,WordPress,PHPwind,Discuz在Nginx下的rewrite规则

PHPCMS:

location / {
###以下为PHPCMS 伪静态化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;
}
商城shopex

location / {
if (!-e $request_filename) {
rewrite ^/(.+\.(html|xml|json|htm|php|jsp|asp|shtml))$ /index.php?$1 last;
}
}
ECSHOP

Nginx环境下supesite discuz wordpress rewrite 规则

在VPS上装的是口碑较好的 nginx,即LNMP. 但nginx下静态化规则与apache不同,需要重新配置,我目前用到的几款程序的静态化规则如下:wordpress 、supesite、discuz,nginx下静态化规则 ctrl+c +v following

//supesite nginx 静态化规则
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;

在线生成阻止某一国家的IP访问的.htaccess代码

http://www.blockacountry.com/

选择国家,再点go~
生成的代码添加到.htaccess里即可。

Nginx下Discuz!的Rewrite

Discuz!在Nginx下的Rewrite
需要说明的是,下网上以前一直流传的Rewrite都是有误的。
下面的Rewrite中百分号前面多了个转移字符“\”,这在Apache中是需要的,而在Nginx中则是不需要的。
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page\%3D$3&page=$2 last;
正确的应该是
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;

使用.htaccess实现图片防盗链

统一在网站根目录下的.htaccess设置,虽然理论上而言,在Apache中,可以针对每个目录分别设置.htaccess,通过许可权的继承 与覆盖可以实现相当复杂的功能,不过,过多的.htaccess往往会增加管理的难度,修改.htaccess稍有遗漏就可能造成网站出现问题。

默认情况想禁止其它网站盗链,对图片而言,当其他网站使用<img src=”http://yourdomain.com/someimg”>盗链时,会自动重定向,当然,从降低对服务器带宽占用的角度考虑,你可 以简单地拒绝其访问,让其网页上图片位置以红”X”号代替。

允许特定访问来源:单纯针对图片来说,我们也不可能禁止所有除你自己网站之外的其他访问,比如说Google,如果你希望通过Google图片搜索 获得一定的访问的话,必须让其能够正确读取真正的图片,再如应能够让RSS订阅用户看feed中的图片。

运行特定目录下的文件被外部网站使用:完全禁止外部网站有时会带来不便,很大时候,我们自己也可能需要在外部网站使用部分文件,当然,放入这些目录 的文件要有一定的限制,不然,便失去设置防盗链的意义了。
设置.htaccess禁止图片盗链,下面是禁止图片盗链的.htaccess设置部分

Nginx下Discuz!的Rewrite

location / {
index index.html index.htm index.php;
root /www/www.typengine.com;
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;

给nginx加rewrite跳转

nginx中如果一个server节点有多个域名,并且在不是访问主域名的情况下如果进行rewrite跳转,会自动把域名转换成主域名。比如:

同步内容