华纳云香港服务器

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

WordPress主题推荐

不少朋友总是询问 WordPress 如何添加伪静态规则,今天就总结一下 IIS/Apache/Nginx 三种环境下的伪静态规则,希望对大家有所帮助。

检测主机是否支持伪静态的方法:在 WP 后台 – 设置 – 固定链接,设置为非默认带?的那种结构,然后访问任何一篇文章,如果出现 404 错误,说明你的主机当前不支持 WordPress 伪静态。

IIS/Apache/Nginx实现WordPress伪静态的办法-第1张-boke112百科(boke112.com)

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 站点的根目录即可。

Apache伪静态规则

Apache 是 Linux 主机下常见的环境,现在一般的 Linux 虚拟主机都采用这种环境。新建一个 htaccess.txt 文件,添加下面的代码:

  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>

然后上传到 WordPress 站点的根目录,重命名为 .htaccess 即可

Nginx伪静态规则

Nginx 环境一般是 Linux 主机 VPS 或服务器用户用的比较多,这些用户一般都会自己配置 Nginx,或者有专门的人帮你配置,打开 nginx.conf 或者某个站点的配置环境,比如 wpdaxue.com.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. }

保存,重启 Nginx 即可。

题外话:倡萌一直不推荐在 windows 的 IIS 服务器下安装 WordPress,因为 IIS 环境运行 php 程序的效率,相对同等配置下 Linux 的 Apache 和 Nginx 环境,要低的多。

赞 (0) 打赏
版权声明:本文为转载文章,来源于 WordPress大学 ,版权归原作者所有!发布此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请联系我们,确认后马上更正或删除,谢谢!
wu