zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    11. 盛最多水的容器
    820. 单词的压缩编码
    72. 编辑距离
    10041-回文数
    第六章-希尔排序和堆排序
    第六章-快速排序、合并排序
    第六章 排序-冒泡,选择,插入排序
    第四章-二叉树的运用-AVL平衡二叉树
    第四章-二叉树的运用-排序二叉树的代码
    第四章-二叉树的代码 、二叉树前序中序后序遍历、及运用
  • 原文地址:https://www.cnblogs.com/52fhy/p/5054507.html
Copyright © 2011-2022 走看看