Nginx下WordPress的Rewrite
Apache
在Apache下,利用mod_rewrite来实现URL的静态化。
.htaccess的内容如下:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Nginx
在上次《Nginx的Rewrite配置》中有个朋友问WordPress如何配置Rewrite,当时也没给个完整正确的答案,最近自己需要Nginx下配置,不得不去解决这个问题。
其实在Nginx下配置WordPress的Rewrite还是比较简单的,在location /{………………}里面加入
if (!-f $request_filename){
rewrite (.*) /index.php;
}
即可实现。
下面是一个完整的vhost的配置文件
server {
listen 80;
server_name ccvita.com www.ccvita.com;
location / {
index index.html index.htm index.php;
root /www/wwwroot/ccvita.com;
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$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:8787;
fastcgi_param SCRIPT_FILENAME /www/wwwroot/ccvita.com$fastcgi_script_name;
}
location /ccvita-status {
stub_status on;
access_log off;
}
}
listen 80;
server_name ccvita.com www.ccvita.com;
location / {
index index.html index.htm index.php;
root /www/wwwroot/ccvita.com;
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$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:8787;
fastcgi_param SCRIPT_FILENAME /www/wwwroot/ccvita.com$fastcgi_script_name;
}
location /ccvita-status {
stub_status on;
access_log off;
}
}
相关文档:
《Nginx下Discuz!的Rewrite》:http://www.ccvita.com/348.html
《Nginx下WordPress的Rewrite》:http://www.ccvita.com/336.html
《Nginx的Rewrite配置》:http://www.ccvita.com/319.html
《Nginx的防盗链配置》:http://www.ccvita.com/312.html
if (!-e $request_filename) {
rewrite ^([_0-9a-zA-Z-]+)?(/wp-.*) $2 last;
rewrite ^([_0-9a-zA-Z-]+)?(/.*\.php)$ $2 last;
rewrite ^ /index.php last;
}
我的是这样配置的。
没有评论
@joyqi 看见上面那条评论没有,只是还没有审核而已嘛
IIS下好像不行。
@张家口 这不是IIS下的规则嘛~~~~
WP可以直接自定议REWRITE
WP是可以直接自定议REWRITE,但是你服务器得支持啊
WP是可以直接自定议REWRITE,但是你服务器得支持啊
你好,能帮忙看下我的重写配置有什么问题吗?
我想用nginx实现和apache里一样的重写规则
这个是我的.htaccess的重写配置
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
nginx配置
server {
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /www/wwwroot;
location / {
if (!-e $request_filename){
rewrite (.*) /index.php;
}
}
location ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
access_log off;
}
location /status/ {
stub_status on;
access_log off;
}
}
@miles, 文中有提到在location /{………………}里面加入
if (!-f $request_filename){
rewrite (.*) /index.php;
}
即可实现。
我按照博主写的配置的
但是结果是
Error 404 - Not Found
博主你好,我是个新手
按照博主的方式配置之后,WP固定链接,其他形式都可用
但是我最想用的postname形式,总是404错误。
让人郁闷的是,我测试的时候,postname是好使的,把文章都导入后发现出现404了,真是诡异啊
请博主帮忙分析下原因吧,感激不尽
回复留言
水水语录
你脑袋有坑,像月球表面一样
最新日志
分类列表
文章归档
关键词
Apache 媳妇 配置 感慨 PHP技术 MySQL Memcache 优化 PHP类 css PHP 迪拜 陈陈 调优 Linux 生活 MySQL优化 FireFox Discuz! Nginx 缓存 性能 Rewrite MooPHP框架 JavaScript WordPress插件 WordPress MySQL效率 风骚 生日 奇矩互动 负载 视频 blog程序 IE6 分析 yy 高负载 ZIP IE7 服务器 正则表达式 常用 防盗链 Cookie