Miscellaneous

How to 301 Redirect all Subdomains

March 15, 2011   ·   By   ·   1 Comment   ·   Posted in Miscellaneous

Just a quick post – I recently had a friend who was moving from a WP MultiUser setup, where each blog had its own subdomain, to one consolidated blog. He wanted to maintain links pointed to his old blogs, so he needed to 301 redirect all the pages on the old subdomains to the appropriate pages on the main domain using his .htaccess file. The code to put it together wasn’t too tricky:

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

This will take any request your site receives, and make sure that the url starts (after the http://) with yourdomain.com. If it starts with anything else (say blog.yourdomain.com, or even www.yourdomain.com), it will be redirected to http://yourdomain.com. This code also allows for deep redirection – that is, if the user typed in

blog.yourdomain.com/my-favorite-page/

they’ll be redirected to

yourdomain.com/my-favorite-page/

which is definitely what you want.

One Comment
  1. Eric

    This was actually useful, thank you.

Submit a Comment