0

我遇到了这个奇怪的问题,每当我添加这个最常用的 htaccess 代码时,我都会得到太多的重定向。

设想:

+public_html
|
|--- WordPress 1
|--- .htaccess
 --+ /blog
   |
   |--WordPress 2
    --.htaccess

.htaccess (WordPress 2)

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.test/blog/$1 [R=301,L]

# 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
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#RewriteCond %{HTTPS} off
#RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
  
</IfModule>

# END WordPress

每当我在任何 htaccess 中添加 HTTPS 重定向代码时,它都会提供太多重定向。

似乎 RewriteCond %{HTTPS} 关闭这种情况总是正确的,这就是为什么(在这里检查:http: //htaccess.madewithlove.be/)我该如何解决这个问题?

注意:WordPress 主页/站点 URL 设置正确:WordPress 2 的https://domain.test/blog

编辑:解决方案:问题是 Cloudflare 中的灵活模式。当我在 Cloudflare 中将其设置为完全 SSL 模式时,问题就解决了。

4

2 回答 2

0

问题是 Cloudflare 中的灵活模式。当我在 Cloudflare 中将其设置为完全 SSL 模式时,问题就解决了。

于 2021-04-20T11:39:35.023 回答
0

在你的文件 '.htaccess Wordpress 2 你有一个错误。

RewriteRule ^(.*)$ https://www.domain.test/blog/$1 [R=301,L]

这给你带来了问题。怎么了:

  1. 说,你的输入是http://www.domain.test/blog
  2. 该行将RewriteCond %{HTTPS} off触发,因为您正在使用http而不是https
  3. 意味着执行 newxt 重写规则。

这部分:^(.*)$表示:

  1. 将所有内容从开始 ( ^) 到结束 ( $)。(在你的情况下是blog。)

  2. 将该部分放入变量中。那是括号部分的(.*)意思。它将使“一切”部分作为变量可用$1

  3. 把这些放在一起,你现在应该$1blog. 最终结果是:

    https://www.domain.test/blog/blog/

因此,您应该首先删除blog第三行的部分:

RewriteRule ^(.*)$ https://www.domain.test/$1 [R=301,L]

可能会出现新问题,但您必须先解决此问题。

于 2021-04-20T10:55:08.820 回答