There are questions remain, We'll search for the answers together. But one thing we known for sure,the future is not set!

[原创文章]WDCP v3网站开启伪静态及转换.htaccess为nginx伪静态文件conf的方法

系统防护 百蔬君 6485℃ 已收录 0评论

WDCP v3正式登场了,个人感觉是一款非常好用的免费linux的web环境搭建和web管理后台程序,最近安装了wdcp的最新版,支持多个php环境,但是在使用过程中发现一个问题,网站的伪静态没有工作。使用的是nginx+apache的环境,经过深入研究发现.htaccess没有上班,原来在v2版本的时候,把这个文件放到网站根目录就好了,但是v3版本失效。

对于v3版本,正确的设置伪静态的方法是在WDCP V3管理后台的“网站管理”的”rewrite规则管理“处,建立conf文件。按照规则“以.conf结尾,且文件名中包括_apache或_nginx,如discuz_apache.conf,代表apache或nginx的文件”建立相应的conf规则。至于“.htaccess”伪静态方式在单独使用apache环境时和nginx+apache环境都是有效的。

由于我使用的环境是前台nginx,后台是apache,那么这里的网站的伪静态规则应当是适合nginx的,而不是apache的,这是v3版本与v2版本的不同。由于之前一直都是使用.htaccess来设置伪静态,那么这里就涉及到一个把apache的伪静态文件“.htaccess”转换为nginx的conf文件的问题。

就拿我做的这个站做一个实例吧。
apache伪静态文件“.htaccess”的内容为:

<FilesMatch "\.(bak|inc|lib|sh|tpl|lbi|dwt)$">
    order deny,allow
    deny from all

ErrorDocument 404 /404.php
RewriteEngine On
RewriteBase / 
RewriteCond %{HTTP_HOST} ^baishujun\.com$ 
RewriteRule ^(.*)$ https://www.baishujun.com/$1 [R=301,L]
RewriteRule ^index\.html$    index\.php [L]
RewriteRule ^category$      index\.php [L]
# access any object by its numeric identifier
RewriteRule ^feed-c([0-9]+)\.xml$       feed\.php\?cat=$1 [L]
RewriteRule ^feed-b([0-9]+)\.xml$       feed\.php\?brand=$1 [L]
RewriteRule ^feed-type([^-]+)\.xml$       feed\.php\?type=$1 [L]
RewriteRule ^feed\.xml$                 feed\.php [L]
RewriteRule ^buyer-show\.html$                 buyer_show\.php [L]
RewriteRule ^blogger\.html$                 ads\.php [L]
RewriteRule ^(.*)-c([0-9]+)-under-([a-z0-9]+)-dollar.html$						category\.php\?id=$2&utxt=$3  [QSA,L]
RewriteRule ^(.*)-c([0-9]+)$						category\.php\?id=$2  [QSA,L]
RewriteRule ^(on-sale|new-arrival|best-items|feature-product|clearance|under-5|under-10|100-in-stock)-([0-9]+)-([0-9]+)-([0-9.]+)-([0-9.]+)-([a-z]+)-([a-z_]+)-([A-Z]+)-([0-9]{1})\.html$	special\.php\?act=$1&cat_id=$2&page=$3&price_min=$4&price_max=$5&display=$6&sort=$7&order=$8&s=$9 [QSA,L]
RewriteRule ^daily-([0-9]{4}-[0-9]{2}-[0-9]{2})\.html$		daily\.php\?day=$1 [QSA,L]

转换为baishujun_nginx.conf后为:

rewrite ^/index.html$    /index.php last;
rewrite ^/category$      /index.php last;
rewrite ^/((?!guanzhou020|js|themes|print|facebook|edm).)*/(.*) /index.php last;
rewrite ^/feed-c([0-9]+).xml$       /feed.php?cat=$1 last;
rewrite ^/feed-b([0-9]+).xml$       /feed.php?brand=$1 last;
rewrite ^/feed-type([^-]+).xml$       /feed.php?type=$1 last;
rewrite ^/feed.xml$                 /feed.php last;
rewrite ^/buyer-show.html$                 /buyer_show.php last;
rewrite ^/blogger.html$                 /ads.php last;
rewrite ^/(.*)-c([0-9]+)-under-([a-z0-9]+)-dollar.html$	/category.php?id=$2&utxt=$3  last;
rewrite ^/(.*)-c([0-9]+)$						/category.php?id=$2  last;
rewrite "^/(on-sale|new-arrival|best-items|feature-product|clearance|under-5|under-10|100-in-stock)-([0-9]+)-([0-9]+)-([0-9.]+)-([0-9.]+)-([a-z]+)-([a-z_]+)-([A-Z]+)-([0-9]{1}).html$" /special.php?act=$1&cat_id=$2&page=$3&price_min=$4&price_max=$5&display=$6&sort=$7&order=$8&s=$9 last;
rewrite "^/daily-([0-9]{4}-[0-9]{2}-[0-9]{2}).html$"		/daily.php?day=$1 last;

这里有几点需要注意,记录下来,和大家分享。
1,RewriteRule需要全部替换为rewrite
2,替换掉所有的\,conf不需要转义符。
3,conf文件的规则开头需要加上/
4, 所有的[L] [QSA,L]等需要替换为 last
5,每一行必须以;结尾。
上面5点实例
RewriteRule ^category$ index\.php [L]需要更换为rewrite ^/category$ /index.php last;
6,对于包含|或者}等特殊字符的规则,必须用""引起来,不然会当作规则的终止符号。
rewrite ^/daily-([0-9]{4}-[0-9]{2}-[0-9]{2}).html$ /daily.php?day=$1 last;,需要用双引号括起来rewrite "^/daily-([0-9]{4}-[0-9]{2}-[0-9]{2}).html$" /daily.php?day=$1 last;,不然会出错。

对于nginx伪静态规则的调试,有一个小建议,就是登陆ssh,使用service nginxd restart重启,如果规则不对,就会无法启动nginx,这是最好的调试方法了。

规则正确了,也就重启成功了。

转载请注明:百蔬君 » [原创文章]WDCP v3网站开启伪静态及转换.htaccess为nginx伪静态文件conf的方法

喜欢 (2)or分享 (0)
发表我的评论
取消评论

请证明您不是机器人(^v^):

表情