Ramblings & ephemera

Preserve links after a website move with mod_rewrite

My blog was at http://www.granneman.com/blog, but I then moved it, after several years of living at its old address, to http://blog.granneman.com. I wanted to preserve all my links, however, so that someone going to http://www.granneman.com/blog/2008/04/20/after-a-stroke-he-can-write-but-cant-read/ would instead end up at http://blog.granneman.com/2008/04/20/after-a-stroke-he-can-write-but-cant-read/.

To do this, I edited the .htaccess file in http://www.granneman.com/blog to read as follows (For =LT=, substitute a < , and for =GT=, substitute a >):

=LT=IfModule mod_rewrite.c=GT=
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^granneman.com$ 
RewriteRule ^(.*)$ http://blog.granneman.com/$1 [R=301,L] 
RewriteCond %{HTTP_HOST} ^www.granneman.com$ 
RewriteRule ^(.*)$ http://blog.granneman.com/$1 [R=301,L]

Works perfectly.

Comments are closed.