Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将单个页面从其他域重定向到主域会导致重定向过多_Php_Apache_.htaccess_Mod Rewrite_Redirect - Fatal编程技术网

Php 将单个页面从其他域重定向到主域会导致重定向过多

Php 将单个页面从其他域重定向到主域会导致重定向过多,php,apache,.htaccess,mod-rewrite,redirect,Php,Apache,.htaccess,Mod Rewrite,Redirect,我只需要将另一个域上的/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.co

我只需要将另一个域上的
/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]
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]
我看到两个问题

A

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

通常,您会通过添加此条件(顶部第一行)来更改第一条规则:

但在我看来,您可以完全删除该规则,因为您已经使用该规则强制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]
一个条件仅适用于一条规则。现在,这会导致otherdomain上的文件重定向到它们自己

此时,您需要注释掉这一行:

RewriteRule ^(.*)$ http://www.otherdomain.com/$1 [R=301,L]
好的,试试这个。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]
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缓存。

/login/
目录吗?@anubhava Nope,没有目录。取消注释
/login
规则,然后打开Firebug并访问
http://otherdomain.com/login
page查看需要多少重定向happening@anubhava在Firefox切断无限循环之前,大约有21个重定向。好的,但这些重定向是什么?他们是否正在重定向到
http://otherdomain.com/login
http://domain.com/login
来回?仍然会收到太多重定向。我尝试了你的两种解决方案。继续重定向到
otherdomain.com/login
在答案中添加了B部分:当应用B部分时,它只是停留在
otherdomain.com/login
上,现在每个页面都重定向到
domain.com
。只有
/login
需要重定向。几乎!在
otherdomain.com
上还没有
www
-修复。