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 }
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.
Thanks,
but i want to redirect from example.com to example.com/blog
,,,??!
@XL I’d give this a shot, I haven’t tested it myself but I’m about 99% sure on it!
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.
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?
Thanks for this post, exactly what I was looking for.
It’s not working when someone type your url without the ‘www’ text in the address bar.
ex: ninedays.org/blog
yep, it doesn’t work without www :/ Terri, did you get such problem ?
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/”
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.
Thanks! I’ve searched it for a long time. I hate htaccess. ;-)
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)
Thank you so VERY much! You are a genius. Save me lots of time.
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.
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 WordPressEND WordPress
Of course, substitute the actual name of your directories for “directory” and “nutherdirectory” and you’re good to go!
Cheers!
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
Hey, this worked great. My damn hosting company wanted to charge $5 a month for a subdomain. Thanks a lot :)
{ 1 trackback }