Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to forward existing subdomain links to the root domain as the subdomain no longer exists. I have tried so many different lines of code in my .htaccess file and can not get the redirect to work.

The links to the subdomain look like: http://sub.mydomain.com/dir/y99bb4

Can anyone suggest any code for me?

share|improve this question

To redirect requests for sub.yourdomain.com/uri to yourdomain.com/uri:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.yourdomain.com$
RewriteRule ^(.*)$ http://yourdomain.com/$1 [R=301,L]

To redirect requests for sub.yourdomain.com/uri to yourdomain.com:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.yourdomain.com$
RewriteRule ^(.*)$ http://yourdomain.com/ [R=301,L]
share|improve this answer
Thanks for your reply Nick. I just need to redirect from sub.yourdomain.com/uri to yourdomain.com (no uri) – Lin Aug 17 '12 at 9:48
@Lin Thanks for the information. I've updated my answer with a rewrite rule that drops the path when redirecting. – Nick Aug 17 '12 at 9:57
thanks but it's not working for me. Maybe I'm placing it in the wrong spot in my .htaccess # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On [I tried the code here] RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress [I also tried the code here] other working 301s are here I can't seem to format this so it's easy to read. – Lin Aug 18 '12 at 3:08
@Lin I've tested this on a live site and the rules are working for me. Is there still a DNS record for the subdomain in your domain control panel? Or, if you're using nameservers and managing the subdomain with cPanel or similar, is there still a subdomain entry in your cPanel account? Removing either of these will break the redirection because there still needs to be a record that the subdomain existed. Let me know if that's not clear and I'll try to help. – Nick Aug 19 '12 at 12:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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