Wordpress / 整活 · 11月 18, 2019 0

WordPress更改固定链接404的解决办法

很多站长在玩 WordPress 的时候,可能会碰到一个问题,就是想把 WordPress 伪静态,在后台设置好固定链接之后,就会出现文章页面或者所有的页面都出现 404 错误。下面就提供各种 web 环境下的 WordPress 伪静态规则设置教程。

https://qq52o.me/1876.html

Apache 伪静态规则

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

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

然后上传到 WordPress 站点的根目录,重命名为 .htaccess ,修改完成后,要重启 Apache 才能生效。

Nginx伪静态规则

打开nginx.conf或者某个站点的配置环境,比如henchat.ml(不同的网站配置不一样),在server {}大括号里面添加下面的代码:

location / {  
    index index.html index.php;   
    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;      }   
}   
rewrite /wp-admin scheme://host$uri/ permanent;

保存以后,重启 Nginx 即可。

IIS 伪静态

强烈不推荐在 windows 的 IIS 服务器下安装 WordPress,因为 IIS 环境运行 PHP 程序的效率,相对同等配置下 Linux 的 Apache 和 Nginx 环境,要低的多,更甚至于坑太多!

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

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

参考