将子域重定向到subdomain.domain.com/mypage.html

时间:2014-09-24 19:51:12

标签: apache .htaccess

我搜索过类似的问题,但找不到与我需要的完全匹配。

我有一个通配符子域设置,我想将所有访问者重定向到 anysubdomainhere.maindomain.com到anysubdomainhere.maindomain.com/mypage.html

我也希望所有访问过没有子域名前缀的访问者都能正常行事。

我的htaccess中有以下内容,但我在浏览器中收到“太多重定向”的错误。

RewriteCond %{HTTP_HOST} ^([^/.]+)\.maindomain\.com$ 
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule (.+)$ "http://%1.maindomain.com/mypage.html" [L,P]

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

您可能不希望P标志。用于代理。您希望R标志用于重定向。

循环可能是因为/mypage.html 也匹配正则表达式(.+)$这一事实,因此您需要添加例外:

RewriteCond %{HTTP_HOST} ^([^/.]+)\.maindomain\.com$ 
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteCond $1 !mypage\.html
RewriteRule (.+)$ "http://%1.maindomain.com/mypage.html" [L,R]

答案 1 :(得分:0)

那是因为当访问者到达mypage.html时,您再次重定向(并再次)。 您可以将其更改为:

RewriteCond %{REQUEST_URI} !^/mypage.html [NC]
RewriteCond %{HTTP_HOST} ^([^/.]+)\.maindomain\.com$ 
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule (.+)$ "http://%1.maindomain.com/mypage.html" [L,R]

(注意我此时无法测试,因此上面可能包含错误)

另请注意,当访问者访问另一个页面后,您将再次重定向到mypage.html。 所以也许您想要使用以下规则:

RewriteCond %{REQUEST_URI} !^/$ [NC]

在这种情况下,您可以将用户重定向到"主页"仅

相关问题