.ht访问 Wordpress 目录中的冲突



我的Web服务器上的公共文件的根目录中有一个htaccess文件:

Header set Content-Security-Policy: upgrade-insecure-requests
DirectoryIndex index.html index.php parking-page.html
RewriteEngine on
# Force www
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Force SSL
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
Options -Indexes
ErrorDocument 404 https://%{HTTP_HOST}/index.php
# for JWT auth
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
# end for JWT auth

我在/blog 上安装了一个 Wordpress 博客,Wordpress 在该文件夹中创建了一个 .htaccess 文件:

# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress

我注意到,当我访问 http://example.com/blog 时,它会将我重定向到 https://www.example.com/index.php,而它应该转到 https://www.example.com/blog。 其他页面(例如 http://example.com/contact-us(成功重定向到 https://www.example.com/contact-us

更新:

  • https://example.com/blog 工作正常(重定向到 https://www.example.com/blog(。
  • http://www.example.com/blog 已损坏(重定向至 https://www.example.com/index.php(。

重写规则中可能有错误,无法从非 www 重定向到 www。我不知道你从哪里得到的代码,但我以前从未见过这样的尝试。

您可以将强制使用 www 和 SSL 的单独规则替换为一个,如下所示:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=302]

重要提示:请注意使用 302重定向 - 在确认它有效之前,您应该只使用临时重定向。只有当您知道它正在工作时,才应将其更改为 301。

由于您在当前规则中使用了 301 重定向,因此它将被缓存,可能需要一段时间才能清除。

请注意,对于安装在子文件夹中的WP,您需要在子文件夹的.htaccess文件中添加此重写规则。


请注意,您的重写规则未被触发...它看起来只是因为它有效,因为您在 WP 设置中有 https 和 www 作为站点 URL。

因此,http://example.com/bloghttps://www.example.com/index.php的原因可能不是因为重写,而是因为.htaccess中的以下行被触发:

ErrorDocument 404 https://%{HTTP_HOST}/index.php

如果进行上述更改不起作用,则可以将其视为可能的原因。

最新更新