【问题标题】:Redirecting single page from other domain to main domain causes too many redirects将单个页面从其他域重定向到主域会导致重定向过多
【发布时间】:2014-07-02 10:29:38
【问题描述】:

我只需要将另一个域上的 /login 页面重定向到我的主域。

然而,我目前拥有的东西会导致重定向过多。

我错过了什么?

.htaccess:

RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^otherdomain.com$ [NC]
# RewriteRule ^login https://www.domain.com/login [R=301,L]  GIVES TOO MANY REDIRECTS
RewriteRule ^(.*)$ http://www.otherdomain.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^domain.com$
RewriteCond %{HTTPS} !=on
RewriteRule ^/? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [L,QSA]

【问题讨论】:

  • /login/ 是目录吗?
  • @anubhava 不,没有目录。
  • 取消注释 /login 规则,然后打开 Firebug 并访问 http://otherdomain.com/login 页面以查看发生了多少重定向
  • @anubhava 在 Firefox 切断无限循环之前大约有 21 次重定向。
  • 好的,但是这些重定向是什么?他们是否来回重定向到http://otherdomain.com/loginhttp://domain.com/login

标签: php apache .htaccess mod-rewrite redirect


【解决方案1】:

好的,试试这个 .htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?otherdomain\.com$ [NC]
RewriteRule ^login https://www.domain.com/login [R=301,L,NC]

RewriteCond %{HTTP_HOST} ^otherdomain\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [L,QSA]

并确保在新浏览器中测试以避免浏览器的 301 缓存。

【讨论】:

  • 现在每个页面都重定向到domain.com。只有/login 需要重定向。
  • 几乎!在otherdomain.com 上还没有www-fix。
【解决方案2】:

我发现了两个问题。

一个。

您的 https 重定向不会检查您是否已经在使用 https。因此,在重定向到https://www.domain.com/login 之后,您的https 规则就会生效。

通常,您可以通过添加以下条件(顶部的第一行)来更改您的第一条规则:

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

但在我看来,您可以完全删除该规则,因为您已经使用此规则强制使用 https:

RewriteCond %{HTTP_HOST} ^domain.com$
RewriteCond %{HTTPS} !=on
RewriteRule ^/? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

尝试一下:取消注释登录规则,并注释掉第一个 https 规则。

B.第二个问题是 otherdomain.com 重定向到自己:

RewriteCond %{HTTP_HOST} ^otherdomain.com$ [NC]
# RewriteRule ^login https://www.domain.com/login [R=301,L]  GIVES TOO MANY REDIRECTS
RewriteRule ^(.*)$ http://www.otherdomain.com/$1 [R=301,L]

一个条件只适用于一个规则。现在,这导致其他域上的文件重定向到自己。

此刻你需要注释掉这一行:

RewriteRule ^(.*)$ http://www.otherdomain.com/$1 [R=301,L]

【讨论】:

  • 重定向仍然太多。我尝试了您的两种解决方案。不断重定向到otherdomain.com/login
  • 在答案中添加了 B 部分。 :)
  • 在应用 B 部分时,它只是停留在otherdomain.com/login
猜你喜欢
  • 1970-01-01
  • 2021-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-11
相关资源
最近更新 更多