Apache mod_rewrite

This entry was posted by Tuesday, 7 April, 2009
Read the rest of this entry »

As you may notice, debian.co.nz has new urls are cleaner, and look like static html.
This is not to do with the actual files or any major configs, but more to do with mod_rewrite, an apache module.

Basicly it takes the incomming URL and translates it into what it should be using basic regex.

The old news url looked something like this
/news.php?id=31
The newer one looks more like this
/news/31

Some advantages of this:
a) Some search engines dont like having file.php?foo=bar and rank them lower because of it. This will create the LOOK of static html pages.
b) People have to guess what language its written in before they can attempt ‘hacking’ it.
c) The URLs are much friendlier to look at or paste to others.
d) Nice easy URLs to remember 🙂

Okay, Now onto HOW i did it.
Into my httpd.conf under the virtualhost that I am using I added.

RewriteEngine On
RewriteRule ^/news/([0-9]+) /news.php?id=$1 [NC]

The First line makes sure the rewrite engine is on, its fairly self explainatory.
The second line is the heart of it. It takes any incomming urls at /news/number and turns them into /news.php?id=number .

The NC is part of the flags you can parse it, which mean nocase, its not case sensitive.
There are several lots of flags available, and they are definatly worth looking into.

From there you can add in a whole lot more of conditions with RewriteMap Or RewriteCond

This is only a basic overview, there is an awesome tutorial here that can take you more in depth.

Liz
Last-Modified: 2007-03-07 19:38:50


Leave a Reply