Redirect any sub domain to a page on main domain

46,671

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]
Share:
46,671
Andy
Author by

Andy

A teenager with a passion for all things computers! I have cerebral palsy so computers have been my best, and I've got the most out of them. I've taught myself AutoIt, Java, and a bit of most web languages, including PHP and HTML, as now own about six domains and I'm in the process of starting my own business. I don't confess to know everything about anything and I know I have A LOT to learn but I do what I can and try to help out where I can. I think I'm the biggest 'Guru' of the family!

Updated on July 05, 2022

Comments

  • Andy
    Andy almost 2 years

    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.

  • Andy
    Andy over 12 years
    Thanks for the answer, but does this only work if a user tries to go to answers.mydomain.com? If so I think you have misunderstood my question. I would like to do this for any subdomain, kind of like a wild card. i.e If the RewriteCond supported wild cards, it may look something like *.mydomain.com
  • Andy
    Andy over 12 years
    Thanks again Jon - just what I wanted! Though it didn't have the full effect I anticipated, that is inevitable and I don't think any amount of .htaccess would resolve the current situation. Oh well - you're still right and it mostly works! In example: I was trying to be crafty and redirect the subdomains cPanel automatically creates when an addon domain is created to a page so it would seem as though there is no subdomain to the average user!
  • Jon Lin
    Jon Lin over 12 years
    If that's the case, replace the R in the brackets with a P.
  • Andy
    Andy over 12 years
    Still no success unfortunately. (I am testing using inPrivate mode on IE 9 - a new session for every change). I think I would need to put some code in the .htaccess file in the root of the subdomains that aren't being redirected that redirects if the user is not visiting the addon domain, i.e is trying to get to the website via the subdomain.? If so, is it possible to do that and how? Also, is it possible to redirect to a page without changing the URL?
  • Momin
    Momin over 6 years
    So if i want to redirect sub-folder to main domain how can i do this?
  • Gem
    Gem almost 6 years
    @JonLin I have website called xyz.com, and added additional store called abc.xyz.com, now i have purchased new domain 123.com. How can i redirect my subdomain as new domain, like if customer enters 123.com it will be redirect to abc.xyz.com, and url should be like 123.com not changing to abc.xyz.com in the url bar.
  • G_Style
    G_Style over 5 years
    This worked beautifully for me. I got my subdomain working in GCP by just adding that to the top of the body of the /etc/apache/default-ssl.conf file. I have my DNS entries using the same IP so when the designated url is used, the redirect catches it and sends you to the subdomain.