建站教程

建站教程

Products

当前位置:首页 > 建站教程 >

WordPress 伪静态规则(IIS/Apache/Nginx)设置方法

GG网络技术分享 2025-03-18 16:05 0


今天总结一下 IIS/Apache/Nginx 三种环境下的伪静态规则,希望对大家有所帮助。检测主机是否支持伪静态的方法:在 WP 后台 > 设置 > 固定链接,设置为 非默认带?的那种结构,然后访问任何一篇文章,如果出现 404 错误,说明你的主机当前不支持 伪静态。

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.  
  6. CacheClockRate 3600
  7. RepeatLimit 32
  8.  
  9. # Protect httpd.ini and httpd.parse.errors files
  10. # from accessing through HTTP
  11. # Rules to ensure that normal content gets through
  12.  
  13. RewriteRule /tag/(.*) /index.php?tag=$1
  14. RewriteRule /software-files/(.*) /software-files/$1 [L]
  15. RewriteRule /images/(.*) /images/$1 [L]
  16. RewriteRule /sitemap.xml /sitemap.xml [L]
  17. RewriteRule /favicon.ico /favicon.ico [L]
  18. # For file-based wordpress content (i.e. theme), admin, etc.
  19. RewriteRule /wp-(.*) /wp-$1 [L]
  20. # For normal wordpress content, via index.php
  21. RewriteRule ^/$ /index.php [L]
  22. RewriteRule /(.*) /index.php/$1 [L]

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

Apache 伪静态规则

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

  1. RewriteEngine On
  2. RewriteBase /
  3. RewriteRule ^index.php$ - [L]
  4. RewriteCond %{REQUEST_FILENAME} !-f
  5. RewriteCond %{REQUEST_FILENAME} !-d
  6. RewriteRule . /index.php [L]

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

Nginx 伪静态规则

Nginx 环境一般是 Linux 主机 VPS 或服务器用户用的比较多,这些用户一般都会自己配置 Nginx,或者有专门的人帮你配置,打开 nginx.conf 或者某个站点的配置环境,比如 wpdaxue.com.conf(不同人配置的不一样),在 server { } 大括号里面添加下面的代码:

  1. location / {
  2. try_files $uri $uri/ /index.php?$args;
  3. }
  4.  
  5. # Add trailing slash to */wp-admin requests.
  6. rewrite /wp-admin$ $scheme://$host$uri/ permanent;

保存,重启 Nginx 即可。

注:如果以上规则不适用,请自己查看 WP 官方文档:https://codex.wordpress.org/Nginx

标签: WordPress 教程

提交需求或反馈

Demand feedback