本文共 2021 字,大约阅读时间需要 6 分钟。
最近接触WP Super Cache
,该插件要求固定链接必须是重写的,故用到Rewrite。
我的是这样配置的:
/usr/local/nginx/conf/rewrite/wordpress.conf
location / { 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; }}
然后在虚拟主机文件里添加
include /usr/local/nginx/conf/rewrite/wordpress.conf;
即可。
完整的vhost的配置文件:
server { listen 80; server_name me.52fhy.com; index index.php index.html index.htm; root /52fhy.com/wordpress/; location / { 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; } } location ~ .*\.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 1h; } access_log /www/log/me.52fhy.com.log;}
2015-12-18 11:42:24更新:
以上rewrite方法导致后台很多菜单无法访问,现更新。原因是:location / {#1if (-f $request_filename/index.html){ rewrite (.*) $1/index.html break;}#2if (-f $request_filename/index.php){ rewrite (.*) $1/index.php;}#3if (!-f $request_filename){ rewrite (.*) /index.php;}}
对于后台http://me.52fhy.com/wp-admin/options-writing.php
这种链接将直接匹配#3
,实际上这时候不需要任何匹配。故可在#2
前添加
if (-f $request_filename){break;}
或者全部更新更新为:
location / {index index.html index.php;if (-f $request_filename) { break;}if (!-e $request_filename) { rewrite . /index.php last;}}
另外,在Apache下,利用mod_rewrite来实现URL的静态化。
.htaccess
的内容如下:
# BEGIN WordPressRewriteEngine OnRewriteBase /RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L]# END WordPress
转载地址:http://spqko.baihongyu.com/