Apache/Nginx/ISS实现WordPress伪静态的办法

  • A+

最近刚刚从emlog程序换到wordpress程序,在WP后台设置固定链接时出现404错误,最后发现原来是没有添加服务器伪静态规则,下面就总结一下 Apache/Nginx/ISS 三种环境下的伪静态规则,希望对大家有所帮助。

Apache/Nginx/ISS实现WordPress伪静态的办法Apache/Nginx/ISS实现WordPress伪静态的办法

Apache/Nginx/ISS实现WordPress伪静态的办法

在WP后台设置固定连接,设置为非默认带?的那种链接形式,若打开文章出现404错误,说明服务器不支持WordPress伪静态。

Apache伪静态规则

Apache是Linux主机下常见的环境,现在一般的 Linux 虚拟主机都采用这种环境,Apache伪静态规则如下:

  1. <IfModule mod_rewrite.c>
  2. RewriteEngine On
  3. RewriteBase /
  4. RewriteRule ^index\.php$ - [L]
  5. RewriteCond %{REQUEST_FILENAME} !-f
  6. RewriteCond %{REQUEST_FILENAME} !-d
  7. RewriteRule . /index.php [L]
  8. </IfModule>

Nginx伪静态规则

Nginx是不支持.htaccess文件的,打开nginx.conf或者某个站点的配置环境,在server{ }大括号里面添加下面的代码:

  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.         }
  11. }

另外给大家分享一个很实用的Apache转Nginx规则工具,http://www.bt.cn/Tools

IIS伪静态规则

IIS 环境是 Windows 主机常用的服务器环境,新建一个 txt 文件,将下面的代码添加到文件中:

  1. [ISAPI_Rewrite]
  2. # Defend your computer from some worm attacks
  3. #RewriteRule .*(?:global.asa|default\.ida|root\.exe|\.\.).* . [F,I,O]
  4. 3600 = 1 hour
  5. CacheClockRate 3600
  6. RepeatLimit 32
  7. # Protect httpd.ini and httpd.parse.errors files
  8. # from accessing through HTTP
  9. # Rules to ensure that normal content gets through
  10. RewriteRule /tag/(.*) /index\.php\?tag=$1
  11. RewriteRule /software-files/(.*) /software-files/$1 [L]
  12. RewriteRule /images/(.*) /images/$1 [L]
  13. RewriteRule /sitemap.xml /sitemap.xml [L]
  14. RewriteRule /favicon.ico /favicon.ico [L]
  15. # For file-based wordpress content (i.e. theme), admin, etc.
  16. RewriteRule /wp-(.*) /wp-$1 [L]
  17. # For normal wordpress content, via index.php
  18. RewriteRule ^/$ /index.php [L]
  19. RewriteRule /(.*) /index.php/$1 [L]

然后另存为 httpd.ini 文件,上传到WordPress站点的根目录即可。

黄 先森

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: