Redirections Finally Done

After hours of trial and error (yes, I didn’t say I’m smart), I’ve finally figured out the proper mod_rewrite codes to redirect visitors to my new domain. Here’s the situation: the blog under the beyondsimplicity.net domain was located at http://beyondsimplicity.net/jnls. I’ve also got permalink set up. It goes something like http://beyondsimplicity.net/jnls/%year%/%month%/%post-name%.

And now, I wanted a little change for the new domain: jnls.net. First off, to make the address as short as possible, I wanted the blog to be at http://jnls.net. Yes, the blog’s at the root folder, not under a “jnls” folder. So here goes the .htaccess codes:

Options +FollowSymLinks

RewriteEngine on
RewriteBase /
#redirects any /jnls/* requests to /*
RewriteRule ^jnls/(.*)$ http://jnls.net/$1 [R=301,L] 

# for the rest, simple domain redirect
RewriteCond %{HTTP_HOST} ^(www.)?beyondsimplicity.net$ [NC]
RewriteRule ^(.*)$ http://jnls.net/$1 [R=301,L]

Two seemingly simple redirects does the job. Most important thing of all, the old permalink structure remains intact. For example: http://beyondsimplicity.net/jnls/2005/11/domain-changing-woes/ will redirect you to http://jnls.net/2005/11/domain-changing-woes/ automatically. Just 3 simple lines of code. Damn, it took me over 2 hours.

2 Responses to “Redirections Finally Done”

  1. David Says:
    November 24th, 2005 at 10:07 am

    Shouldn’t the second part go before the first part? i.e. redirect beyondsimplicity.net to jnls.net first, then redirect jnls.net/jnls/* to /* ?

    I think .htaccess rules have to be read in order. But then you can tell by now I’m not an expert…

  2. Jonathan Ng Says:
    November 25th, 2005 at 8:36 am

    Well, if the 2nd part go before the 1st part, as in redirect beyondsimplicity.net to jnls.net first, then beyondsimplcity.net/jnls/content will be redirected to jnls.net. That’s why the mod_rewrite rules come first, to redirect the content.

    But unfortunately, I think those rules will be useless once my domain expires come Dec 1…

Leave a Reply