首页 > 解决方案 > 启用 NGINX 后,WordPress 博客子目录中的所有帖子都给出 404 Page Not Found

问题描述

在我从 Plesk (Web Pro) 启用 NGINX 后,我在主目录和子目录博客中的所有帖子都会出现 404 Page Not Found 错误。

我在 Plesk 的“附加 NGINX 指令”中放置了以下代码来修复所有 URL,但它只修复了主站点 (example.com) 而子目录 blogs (example.com/tech/, example.com/ mag/ 和 example.com/dispatch/) 仍然给出 404 错误。

# Wordpress Permalinks
if (!-e $request_filename) {
    rewrite ^(.+)$ /index.php?q=$1 last;
}

之后,我删除了代码并放置了以下代码:

if (!-e $request_filename) {
set $test P;
}
if ($uri !~ ^/(plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon|internal-nginx-static-location)) {
set $test "${test}C";
}
if ($test = PC) {
rewrite ^/(.*)$ /index.php?$1;
}

我在其中一个Plesk 官方页面中找到了这段代码,上面写着:

如果 WordPress 安装位于子目录中(例如,“httpdocs/sub-dir”)或者这是基于子目录的 WordPress 多站点网络,请在 /index.php?$1 之前添加 /sub-dir/ 以便它看起来像这样:重写 ^/(.*)$ /sub-dir/index.php?$1; 注意:对于基于子文件夹的 WordPress 多站点网络,为每个子目录添加上述规则。

到目前为止,我得到了这个(不起作用):

if (!-e $request_filename) {
set $test P;
}
if ($uri !~ ^/(plesk-stat|webstat|webstat-ssl|ftpstat|anon_ftpstat|awstats-icon|internal-nginx-static-location)) {
set $test "${test}C";
}
if ($test = PC) {
rewrite ^/(.*)$ /index.php?$1;
rewrite ^/(.*)$ /tech/index.php?$1;
rewrite ^/(.*)$ /mag/index.php?$1;
rewrite ^/(.*)$ /dispatch/index.php?$1;
}

任何帮助都感激不尽!

PS 主网站就像一个魅力。在 example.com/tech/ 和 /mag/ 和 /dispatch/ 中,虽然主页很好,但帖子给了我 404 页面错误。

标签: wordpressnginxurlpleskpermalinks

解决方案


所以经过一个月的搜索,我终于找到了一个很好的解决方案。

首先,登录您的 Plesk(为此使用 Plesk)并转到您网站的“Apache & nginx 设置”页面。在“Additional NGINX Directives”(先修改)中粘贴以下代码:

if (!-f $request_filename){
    set $rule_0 1$rule_0;
}
if (!-d $request_filename){
    set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
    rewrite ^/tech/(.*)$ /subfolder1/index.php?url=$1 last;
}
if (!-f $request_filename){
    set $rule_1 1$rule_1;
}
if (!-d $request_filename){
    set $rule_1 2$rule_1;
}
if ($rule_1 = "21"){
    rewrite ^/dispatch/(.*)$ /subfolder2/index.php?url=$1 last;
}

subfolder1 和 subfolder2 代表每个子目录站点的网站根文件夹。

  • subfolder1代表example.com/subfolder1
  • subfolder2代表example.com/subfolder2

ETC..

对于您要添加的每个子文件夹 WP 站点,只需放置以下代码并将subfolder1更改为您的站点名称。

 if (!-f $request_filename){
        set $rule_0 1$rule_0;
    }
    if (!-d $request_filename){
        set $rule_0 2$rule_0;
    }
    if ($rule_0 = "21"){
        rewrite ^/tech/(.*)$ /subfolder1/index.php?url=$1 last;
    }

PS 转换后的代码需要添加到“Additional NGINX Directives”中。


推荐阅读