一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

WordPress在nginx下的URL重写规则详解

时间:2014-08-22 编辑:简简单单 来源:一聚教程网

配置代码

 代码如下 复制代码

server {
listen       80;
server_name  www.111com.net 111com.net;
access_log  /data/logs/nginx/www.111com.net.access.log  main;

index index.html index.php;
root /data/site/www.111com.net;
location / {
try_files $uri $uri/ /index.php?$args;
add_header Pragma "public";
expires 12h;
}

rewrite /wp-admin$ $scheme://$host$uri/ permanent;

location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
       access_log off;
   log_not_found off;
   expires max;
}


location ~ \.php$ {
try_files $uri =404;

fastcgi_split_path_info ^(.+\.php)(/.+)$;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#       fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 24h;
access_log off;
}

location ~ .*\.(js|css)$
{
expires 24h;
access_log off;
}
 

重启你的nginx即可

固定链接(URL重写)在nginx的规则

只是用于WordPress单站点(一般默认安装的),不是多站点哦。将以下代码插入location的root中

 代码如下 复制代码
if (!-f $request_filename){
set $rule_1 1$rule_1;
}
if (!-d $request_filename){
set $rule_1 2$rule_1;
}
if ($rule_1 = "21"){
rewrite /. /index.php last;
}

我的WordPress在nginx配置情况:

 代码如下 复制代码

location / {
    root   html/xiedexu.cn;
    index  index.php index.html index.htm;
if (!-f $request_filename){
set $rule_1 1$rule_1;
}
if (!-d $request_filename){
set $rule_1 2$rule_1;
}
if ($rule_1 = "21"){
rewrite /. /index.php last;
}
add_header Cache-Control private;
   
}

现在在新版本的已有不妥,会有两个规则警告提示。博友@五月里徜徉的小猫咪 说是nginx的if陷阱,分享了一个新的配置方案。方案比较全,不过如果你的conf不是那样配置的,只需要在location中插入:

try_files $uri $uri/ /index.php?$args;


发下自用的WordPress Nginx重定向规则,适合非子目录安装:

 代码如下 复制代码


location / {
    try_files $uri $uri/ /index.php?$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

AMH自带的为:

 代码如下 复制代码

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;
    }
}

简单使用方法:AMH使用AMRewrite插件新建一条Rewrite规则,校验规则,然后编辑虚拟主机,选择新建的规则。
直接改nginx.conf了解的自然了解,不熟悉的随便就瘫了。

热门栏目