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.

2 Answers 2

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]
7
  • 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 4, 2012 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 4, 2012 at 19:26
  • If that's the case, replace the R in the brackets with a P.
    – Jon Lin
    Jan 4, 2012 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 4, 2012 at 20:28
  • 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 1, 2018 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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.