当前位置:首页 > CMS教程 > WordPress > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2014-10-17 15:56:00 浏览: 评论:0 

下文中整理了在nginx中配置wordpress博客的URL重写规则了,包括了整站配置与固定链接配置了,下面我们就一起来看看吧.

配置代码,代码如下:

  1. server { 
  2. listen       80; 
  3. server_name  www.phpfensi.com phpfensi.com; 
  4. access_log  /data/logs/nginx/www.phpfensi.com.access.log  main; 
  5.  
  6. index index.html index.php; 
  7. root /data/site/www.phpfensi.com; 
  8. location / { 
  9. try_files $uri $uri/ /index.php?$args; 
  10. add_header Pragma "public"
  11. expires 12h; 
  12.  
  13. rewrite /wp-admin$ $scheme://$host$uri/ permanent; 
  14.  
  15. 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)$ { 
  16.        access_log off
  17.    log_not_found off
  18.    expires max
  19.  
  20.  
  21. location ~ \.php$ { 
  22. try_files $uri =404; 
  23.  
  24. fastcgi_split_path_info ^(.+\.php)(/.+)$; 
  25. #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini 
  26.  
  27. include fastcgi_params; 
  28. fastcgi_index index.php; 
  29. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
  30. #       fastcgi_intercept_errors on
  31. fastcgi_pass 127.0.0.1:9000; 
  32.  
  33. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ 
  34. expires 24h; 
  35. access_log off
  36.  
  37. location ~ .*\.(js|css)$ 
  38. expires 24h; 
  39. access_log off

重启你的nginx即可,固定链接(URL重写)在nginx的规则,只是用于WordPress单站点,一般默认安装的,不是多站点,将以下代码插入location的root中,代码如下:

  1. if (!-f $request_filename){ 
  2. set $rule_1 1$rule_1
  3. if (!-d $request_filename){ 
  4. set $rule_1 2$rule_1
  5. //phpfensi.com 
  6. if ($rule_1 = "21"){ 
  7. rewrite /. /index.php last; 

我的WordPress在nginx配置情况,代码如下:

  1. location / { 
  2.     root   html/xiedexu.cn; 
  3.     index  index.php index.html index.htm; 
  4. if (!-f $request_filename){ 
  5. set $rule_1 1$rule_1; 
  6. if (!-d $request_filename){ 
  7. set $rule_1 2$rule_1; 
  8. if ($rule_1 = "21"){ 
  9. rewrite /. /index.php last
  10. add_header Cache-Control private; 
  11.      

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

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

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

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

AMH自带的为,代码如下:

  1. location / { 
  2.     if (-f $request_filename/index.html){ 
  3. rewrite (.*) $1/index.html break; 
  4.     } 
  5.     if (-f $request_filename/index.php){ 
  6. rewrite (.*) $1/index.php; 
  7.     } 
  8.     if (!-f $request_filename){ 
  9. rewrite (.*) /index.php; 
  10.     } 

简单使用方法:AMH使用AMRewrite插件新建一条Rewrite规则,校验规则,然后编辑虚拟主机,选择新建的规则.

直接改nginx.conf了解的自然了解,不熟悉的随便就瘫了.

Tags: WordPress重写规则 URL重写规则

分享到: