【问题标题】:URL Redirect causing loopURL 重定向导致循环
【发布时间】:2014-02-14 13:35:58
【问题描述】:

下面是我的 .htaccess 文件。

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [L]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^axiom/?$ /axiom/publish.htm [L]

这会导致重定向循环。如果我注释掉

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

页面将加载。我不确定我的错误在哪里,但我假设它在这里某处。

【问题讨论】:

  • RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 规则的目的是什么?
  • 将http请求重定向到https。

标签: .htaccess redirect url-rewriting http-redirect


【解决方案1】:

此规则会将您重定向到您正在访问的同一网址 -

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

假设您正在访问http://www.example.com/test/a.html。此重写规则匹配 test/a.html 和

REQUIEST_URI = test/a.html
HTTP_HOST = www.exmpale.com

所以你重定向到https://www.example.com/test/a.html

那么您匹配相同的条件并再次被重定向到同一页面!

如果您尝试从 http 重定向到 https,则需要添加另一个条件来检查它是来自 http 还是 https

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [L]
RewriteRule ^axiom/?$ /axiom/publish.htm [L]

RewriteCond %{HTTPS} ^off$ [NC] 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

【讨论】:

  • 做到了,现在我完全明白发生了什么。感谢您的指导!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多