Using rewrite rules to have a tidy blog 12.28.2006 22:49
Printer Friendly

Today, I was playing with apache mod_rewrite and decided to make my blog a little bit tidier. So I created a rule to make all my links looking like: http://emresaglam.com/blog/[storyID].

Since there are many many tutorials on the web on howto use mod_rewrite I'm not going to go into details.

I created an .htaccess file under my web root directory containing these:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^blog/([0-9]+)$ /blog/haber.php?id=$1 [nc]

So it takes any requests in the form of /blog/[a number] and maps it to the url: /blog/haber.php?id=[the number]

This is a basic regular expression.

And I went into my blog code and changed all the links from the haber.php?id=$id to $id only ; )

That simple!

Update: Murat pointed out that actually this wouldn't work for urls like  /blog/182/ (with a trailing slash) But I already have a rule to redirect the entries with a trailing request with a 301. And then the rules above kicks in. And the rule is:

RewriteRule ^blog/([0-9]+)/$ /blog/$1 [R=301,N]

Back to Eblog

Latest two blog entries:
George Carlin died at the age of 71...
washington, DC has the worst metro system EVER


Make a comment!
Comments will be approved first to prevent suckers to misuse/abuse my comments section. So, there is a delay!
No html tags are allowed. Text is your friend ;)
I will never use your email for bad purposes, so, chill.. ;)

Date: 07.24.2008 17:24
Name:
Email:
URL:
Subject:
Comment:
Please type the number you see in this picture to the box below.