将任何子域重定向到主域上的页面 [英] Redirect any sub domain to a page on main domain
问题描述
真的很快.我希望能够将任何键入 subdomain.mydomain.com
的人重定向到我的主域上的页面.例如,如果我输入:answers.mydomain.com
,我将被重定向到 mydomain.com/suberror
.
Just a quicky really. I want to be able to redirect anybody who types a subdomain.mydomain.com
to be redirected to a page on my main domain. i.e If I typed: answers.mydomain.com
I would be redirected to mydomain.com/suberror
for instance.
如果可能,我希望这是一个通用规则,因为我认为添加语句以排除我不想被重定向的任何子域比添加语句以包含所有其他子域更容易.顺便说一下,我需要使用 .htaccess
文件.
I would like this to be a universal rule if possible because I thought it would be easier to add statements to exclude any subdomains that I didn't want to be redirected rather than add statements to include every other subdomain. I will need to this using a .htaccess
file by the way.
推荐答案
尝试将其添加到 .htaccess 文件中的适当位置:
Try adding this to an appropriate place in your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^answers.mydomain.com$ [NC]
RewriteRule ^ http://mydomain.com/suberror [L,R]
只要请求的主机是 answers.mydomain.com
,就会应用该规则.将正则表达式匹配设置为 ^
,any URI 将匹配并且目标将被重定向到 http://mydomain.com/suberror
As long as the requested host is answers.mydomain.com
, the rule will be applied. With the regex match set to ^
, any URI will match and the target will be redirected to http://mydomain.com/suberror
如果您只想将特定的 URI 请求重定向到/suberror,您可以将 ^
调整为适当的内容.
If you want only specific URI requests to be redirected to /suberror, you can tweak the ^
to something appropriate.
对于所有子域(包括 www.mydomain.com):
For all subdomains (including www.mydomain.com):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+).mydomain.com$ [NC]
RewriteRule ^ http://mydomain.com/suberror [L,R]
要排除 www.mydomain.com,请在 RewriteRule 之前添加以下行:
To exclude www.mydomain.com, add this line before the RewriteRule:
RewriteCond %{HTTP_HOST} !^www.mydomain.com$ [NC]
这篇关于将任何子域重定向到主域上的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!