Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

.htaccess - Redirect any sub domain to a page on main domain

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

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

If you want only specific URI requests to be redirected to /suberror, you can tweak the ^ to something appropriate.

EDIT:

For all subdomains (including www.mydomain.com):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+).mydomain.com$   [NC]
RewriteRule ^ http://mydomain.com/suberror  [L,R]

To exclude www.mydomain.com, add this line before the RewriteRule:

RewriteCond %{HTTP_HOST} !^www.mydomain.com$  [NC]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...