By Lin


2012-08-17 02:16:57 8 Comments

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.example.com/dir/y99bb4

Can anyone suggest any code for me?

1 comments

@Nick 2012-08-17 08:44:01

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

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

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

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

@Lin 2012-08-17 09:48:24

Thanks for your reply Nick. I just need to redirect from sub.yourdomain.com/uri to yourdomain.com (no uri)

@Nick 2012-08-17 09:57:54

@Lin Thanks for the information. I've updated my answer with a rewrite rule that drops the path when redirecting.

@Lin 2012-08-18 03:08:09

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.

@Nick 2012-08-19 12:13:39

@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.

@Paul Shryock 2018-11-12 21:27:18

If I want to use https, would I still use the above code as-is, but with https in the bottom URL, instead of http?

@Nick 2018-11-13 10:35:10

@PaulShryock Yes, that would work.

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Block company domain from apache website how?

  • 2017-11-29 17:22:26
  • rocksusa
  • 96 View
  • 0 Score
  • 1 Answer
  • Tags:   htaccess apache

1 Answered Questions

[SOLVED] Transparent redirection from domain to subdomain

2 Answered Questions

[SOLVED] htaccess redirect subfolder to root

  • 2015-07-05 17:46:08
  • user3371152
  • 837 View
  • 3 Score
  • 2 Answer
  • Tags:   htaccess

2 Answered Questions

[SOLVED] 301 redirect all pages with a specific extension

1 Answered Questions

[SOLVED] Best way to move site from subdomain to root through cPanel?

2 Answered Questions

1 Answered Questions

2 Answered Questions

1 Answered Questions

[SOLVED] SEO: Full Redirect from Subdomain to Root Domain

  • 2011-04-17 15:10:47
  • Jhourlad Estrella
  • 6630 View
  • 1 Score
  • 1 Answer
  • Tags:   htaccess seo

Sponsored Content