【问题标题】:After enabling NGINX, all posts in WordPress blogs in sub-directories are giving 404 Page Not Found启用 NGINX 后,WordPress 博客子目录中的所有帖子都给出 404 Page Not Found
【发布时间】:2020-10-17 15:45:08
【问题描述】:

在我从 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;
}

我在official Plesk pages 之一中找到了这段代码,上面写着:

如果 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;
}

任何帮助将不胜感激!

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

【问题讨论】:

  • 你试过设置->固定链接->保存设置吗?这将刷新 wordpress 重写规则。这也可能对您有所帮助:wordpress.org/support/article/nginx
  • 已经做了,没有帮助。还有其他想法吗?

标签: wordpress nginx url plesk permalinks


【解决方案1】:

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

首先,登录您的 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

等等。

对于您要添加的每个子文件夹 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;
    }

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

【讨论】:

    【解决方案2】:

    尝试通过文件管理器创建一个新文件“.htaccess”或将其上传到 /httpdocs/ 或存储文件的位置。

    文件内容:

    IndexIgnore * # prevent directory listing
    Order deny,allow
    Allow from *
    # ------------------------------------------ # Rewrite so that php extentions are not shown
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php

    【讨论】:

      猜你喜欢
      • 2016-12-23
      • 2018-09-09
      • 2018-11-23
      • 1970-01-01
      • 2023-04-06
      • 2021-12-21
      • 2020-12-05
      • 2018-05-03
      • 1970-01-01
      相关资源
      最近更新 更多