Loading

Nginx下WordPress的Rewrite

最近接触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 / {
    #1
    if (-f $request_filename/index.html){
        rewrite (.*) $1/index.html break;
    }
    
    #2
    if (-f $request_filename/index.php){
        rewrite (.*) $1/index.php;
    }
    #3
    if (!-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 WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

参考:
1、Nginx下WordPress的Rewrite - 平凡的世界
http://www.ccvita.com/336.html
2、wordpress后台访问时没有wp-admin报404错误 | 技术部
http://www.jishubu.net/yunwei/linuxyunwei/420.html

posted @ 2015-12-17 16:20  飞鸿影  阅读(3097)  评论(0编辑  收藏  举报