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

This site runs on the Thesis WordPress Theme

Thesis Theme thumbnail

If you're someone who doesn't understand a lot of PHP, HTML, or CSS, Thesis will give you a ton of functionality without having to alter any code. For the advanced, Thesis has incredible customization possibilities via extensive hooks and filters. And with so many design options, you can use the template over and over and never have it look like the same site.

If you're more familiar with how websites work, you can use the fantastic Thesis User's Guide and world-class support forums to make more professional customizations than you ever thought possible. The theme is not only highly customizable, but it allows me to build sites with a much more targeted focus on monetization than ever before. You can find out more about Thesis below:

{ 25 comments… read them below or add one }

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.

Reply

XL June 30, 2008 at 10:09 am

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

Reply

Terri Ann
Twitter:
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.

Reply

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?

Reply

Vilmis November 17, 2008 at 7:21 pm

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

Reply

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

Reply

Vilmis March 3, 2009 at 6:07 pm

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

Reply

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/”

Reply

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.

Reply

Rutek December 23, 2009 at 4:41 pm

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

Reply

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)

Reply

Suhaimi February 4, 2010 at 8:13 am

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

Reply

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.

Reply

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!

Reply

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

Reply

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

Reply

free seo software October 18, 2010 at 6:22 am

This is really the power of 301 redirects, but if you are on a different platforms other than apache you can used other forms of redirection codes (i.e. php, asp, iis, coldfusion). It maybe a single page cannonical or a domain name cannonical issues but either it can be solved using this 301′s..cheers!

Reply

Irina November 28, 2010 at 7:57 am

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

i inputed this code in the file .htaccess but it does not work(((

also i foung in internet this code

Map http://example.com to /blog.

RewriteRule ^$ /blog/ [L]

Map http://www.example.com/x to /blog/x unless there is a x in the web root.

RewriteCond %{REQUESTFILENAME} !-f
RewriteCond %{REQUEST
FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/wpblog/
RewriteRule ([^/]+)$ /blog/$1

Add trailing slash to directories within /blog

This does not expose the internal URL.

RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^blog/([^/]+)$ http://example.com/$1/ [R=301]

it is code ok only for first page but not good for inner((
what’s wrong?
i try to change link for WP

Reply

Grant
Twitter:
December 31, 2010 at 12:50 am

Thanks for this… I finally got subdomains working!

I did need to change:
RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]

to read:
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

in order to get it working on my site. Other than that, easy peasy.

Thanks again!

Reply

ian January 20, 2011 at 8:49 am

Thanks for your great post.

I used it on my website http://www.antsw.com, everything works fine, but http://antsw.com/blog doesnot work as expected, how to walk around it?
Here is the error message:
“Firefox can’t find the server at http://www.antsw.comblog

Reply

Hamed May 8, 2011 at 1:46 pm

Oh man really thanks you save my life …

Reply

Minimel82 June 8, 2011 at 11:07 am

Seesh! Well if this hasn’t worked for me, then I’m really stuck! My code is:

BEGIN WordPress

Options -Indexes +FollowSymLinks

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

RedirectMatch 301 ^/rickyexplores/(.*)$ http://rickyexplores.puppetsproject.com/$1

END WordPress

This is an actual subdomain website we are trying to get working at the moment. So if you go to http://rickyexplores.puppetsproject.com you’ll see the website is there, but none of the pages work! Now I seen this exact behaviour with wordpress before and all it turned out to be was the apache rewrite module was turned off in my WAMP settings and suddenly all the pages started working.

So when I stumbled across this article on the WP support forum, I thought that must be the problem surely. But it doesn’t seem to be working for me.

The ‘rickyexplores’ is a folder within the root folder for the http://www.puppetsproject.com website.

Any help of thoughts on this? Or am I doing something obviously wrong? I’ve also read peoples comments and made the necessary change to the code where there was a missing ‘/’

I’m real stuck! cheers in advance

Reply

Boniface June 17, 2011 at 2:25 pm

Hi, thanks very much for the post. It is exactly what I was looking for.

@Ian, I’d developed the same error at first. See you have domain. comblog instead od domain .com/blog maybe you messed up with some part of the code and need to rewrite it, refresh the browser cache and see it working see: a-zkenya.info/jobs now redirects to careerjobs-agency.a-zkenya.com/

Reply

Steven Doig October 1, 2011 at 7:54 am

Worked perfectly Terri, thank you.

Reply

Rehmat December 9, 2011 at 10:11 am

Hi All,

i have setup wordpress initially as http://www.example.com/blog , now moved it to a sub-domain in apache like http://blog.example.com, and changed the URL values in WP-Admin Panel.
I setup the .htaccess file in main domain, and added this rule
” RedirectMatch 301 ^/blog/(.*)$ http://blog.example.com/$1
to point old blog URL to the new URL.
Now the the problem is, when i search my blog in google, and then click on the blog articles links, it redirect me to the new blog site, fine till now. but the url in the browser, contains multiple articles names, like this :
http://blog.example.com/ArticleName/?url=blog/ArticleName/
i want to remove the 2nd occurrence of the ArticleName in the URL.

Thanks in advance.

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: