Jan 24, 2013

Clean URL using .htaccess

These days most websites use databases or a content management system (CMS). Some of those web sites or systems have nice URLs like;


But some systems have URLs like;


Both URLs give the same output, but the clean URL is good for search engine optimizations and easy to remember.

If you want to retrieve data from a database according to a input given in the URL you need to use a GET request. So how a clean URL works ?

In a clean URL scenario, web server need to do a redirection. Server will redirect the clean URL request to a not clean URL. Then from the not clean URL, index page will generates the relevant content. But this redirection will not be visible to the user.

 example.com/your-random-page => example.com/index.php?q=your-random-page

So if you change the URL into something else, it will change the value of the parameter in to that like;

 example.com/something-else => example.com/index.php?q=something-else

Then the index page will generate the relevant content according to that value.

TO do these things you need to enable some web server options. First you need to enable '.htaccess'. Then you need to enable 'mode rewrite' mode. '.htaccess' is a part of a server settings file. you can specifically set web server settings via this file and if you do something wrong, that can cause a damage to the webserver too.

Place a '.htaccess' file in the root of you website and add these lines to it.

RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ index.php?q=$1
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?q=$1

1 comment:

Your comments are always welcome ...