21

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
  • 3,600
  • 12
  • 53
  • 84

2 Answers2

31

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]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • 1
    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 Jan 04 '12 at 18:56
  • 1
    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! – Andy Jan 04 '12 at 19:26
  • If that's the case, replace the `R` in the brackets with a `P`. – Jon Lin Jan 04 '12 at 19:35
  • 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? – Andy Jan 04 '12 at 20:28
  • So if i want to redirect sub-folder to main domain how can i do this? – Momin Oct 27 '17 at 03:27
  • @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. – Gem Jun 15 '18 at 05:07
  • 1
    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. – G_Style Nov 01 '18 at 21:35
0

If we are using Linux-based hosting or even shared hosting, in cPanel, while creating a subdomain, we can create a whildcard subdomain and we can specify the path of the system directory.

enter image description here

Above configuration generates following code in .htaccess file

RewriteEngine On
RewriteCond %{HTTP_HOST} ^salebybrands\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.salebybrands\.com$
RewriteRule ^Folder_To_Redirect_Requests\/?(.*)$ "http\:\/\/salebybrands\.com\/$1" [R=301,L]

Now, every subdomain request (for example, abc.salebybrands.com) will be redirect to salebybrands.com/Folder_To_Redirect_Requests

Note: wildcard subdomain will only redirects the unknown subdomains. If you have created other subdomains, they will work as it is and their mapping to the redirection will not be overwritten.

Zaheer
  • 374
  • 4
  • 20