.htaccess Redirect a Directory to a Subdomain and Force WWW

by Terri Ann on March 11, 2008

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

{googlead}

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 ;)

{ 16 comments… read them below or add one }

1 urban bohemian June 4, 2008 at 12:29 pm

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.

2 XL June 30, 2008 at 10:09 am

Thanks,
but i want to redirect from example.com to example.com/blog
,,,??!

3 Terri Ann June 30, 2008 at 10:33 am

@XL I’d give this a shot, I haven’t tested it myself but I’m about 99% sure on it!

Options -Indexes +FollowSymLinks

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

This will ensure (especially important if you are moving your entire blog) that example.com/2008/06/this-blog-name redirects to example.com/blog/2008/06/this-blog-name.

4 Alex Mos October 11, 2008 at 11:33 am

I am having problem redirecting blog.domain.com to http://www.domain.com/blog/

I tried many “htaccess redirect subdomain folder” examples around the web but none seems to be working properly… it redirects to http://www.domain.com without the directory /blog/

any ideea how to make this work?

5 Vilmis November 17, 2008 at 7:21 pm

Thanks for this post, exactly what I was looking for.

6 cwps February 12, 2009 at 12:14 pm

It’s not working when someone type your url without the ‘www’ text in the address bar.
ex: ninedays.org/blog

7 Vilmis March 3, 2009 at 6:07 pm

yep, it doesn’t work without www :/ Terri, did you get such problem ?

8 luke June 10, 2009 at 12:32 am

This is cool..
1 question..
How to make it the other way around.
In my case here.

when go to “blog.example.com” it redirects to
“www.example.com/blog/”

9 Scott Jarvis December 7, 2009 at 12:01 pm

This worked great for me, but slows down my site a bit..

Also, I’d prefer, as above, to always redirect AWAY from http://www.scottjarvis.com to http://scottjarvis.com, which requires another solution.

10 Rutek December 23, 2009 at 4:41 pm

Thanks! I’ve searched it for a long time. I hate htaccess. ;-)

11 Milan December 30, 2009 at 11:24 am

I have been searching and trying solutions for more than half an hour and finally you code fixed problem.

Just to note for others that if you want to redirect from second level directory (eg. example.com/sub/sub2), you should edit .htaccess in root and not in /sub directory because it won’t work.

(btw, you should install Subscribe to Comments plugin for easier tracking of replies on comments)

12 Suhaimi February 4, 2010 at 8:13 am

Thank you so VERY much! You are a genius. Save me lots of time.

13 ASN5 February 20, 2010 at 7:06 pm

Hey there… good stuff!

On a slightly different note… does anyone know of a way to bypass Wordpress for a specific directory? That is, my blog is on the root (http://rootdomain.com) and I’d like to be able to access a directory (http://rootdomain.com/directory) without getting a Wordpress 404.

Any tips would be appreciated.

14 ASN5 March 9, 2010 at 10:57 pm

To answer my own question…

BEGIN WordPress

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(directory|directory/.*)$
RewriteCond %{REQUEST_URI} !^/(nutherdirectory|nutherdirectory/.*)$
RewriteCond %{REQUEST_URI} !^/(transfers|transfers/.*)$
RewriteCond %{REQUEST_URI} !^/(subject|subject/.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

END WordPress

Of course, substitute the actual name of your directories for “directory” and “nutherdirectory” and you’re good to go!

Cheers!

15 ASN5 March 10, 2010 at 11:49 pm

Hey admin…

Sorry to be a pain, but the two extra lines in there are my actual directories:

RewriteCond %{REQUEST_URI} !^/(transfers|transfers/.*)$

RewriteCond %{REQUEST_URI} !^/(subject|subject/.*)$

If you’d whack those out it would probably make it clearer for your following. Same thing for the extra “BEGIN WordPress” and “END WordPress” that are outside the code box.

BTW, I’ve just started hacking on jQuery, so keep the tips coming on that! And while I’m at it, an all-encompassing thanks is in order for generally sharing your work.

Regards,

ASN5

16 Rocky April 8, 2010 at 11:55 pm

Hey, this worked great. My damn hosting company wanted to charge $5 a month for a subdomain. Thanks a lot :)

Leave a Comment

{ 1 trackback }

Previous post:

Next post: