.htaccess Redirect a Directory to a Subdomain and Force WWW
There's nothing like the smell of .htaccess in the morning.
Had to do a quick blog relocation for a friend the other day. The blog used to live at http://www.example.com/blog/ which we moved to the sub domain http://blog.example.com.
Search Engine Friendly
Did I mention, this is using a 301 redirect, which means the spiders will follow it and know, "oh this is where I should look for this file form now on."
It's really easy to set up that redirect in the .htaccess file in the root of example.com, meaning you can completely get rid of the /blog/ directory, which is great cause cluttered folders don't make anyone happy.
While we were in there we added a few lines to force the www to the rest of the domain. This helps ensure you don't get scolded by Google (via page rank) for having duplicate content on example.com/whatever-page.html and www.example.com/whatever-page.html
.htaccess
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
RedirectMatch 301 ^/blog/(.*)$ http://blog.example.com/$1
Geez, I love it when things work quickly and easily!
This works with any blog, or heck even forums: Wordpress, Movable Type, Expression Engine, Geek Log, Simple Machines Forums, vBulletin...you name it! The reason is: because it doesn't care if you have a hand coded manually updated blog all the .htaccess file sees is that someone is trying to access www.example.com/blog/this-post/ and it redirects to blog.example.com/this-post/.
So get going, move stuff around...just for kicks ;)
Popularity: 33% [?]
the newest discoveries, stories and shared tips!Come on, all the cool kids are doing it ;)



Thank you so much! You may have saved me a huge headache. While I work on my primary domain, I created a subdomain for my blog but had to use a fairly wonky 404 page in the meantime.
I'll give this a try. Thanks again.