Genrating SEO friendly url using .htaccess and modrewrite

Here how you can create clean SEO friendly url:

Normal practice of using

url: 1) http://sforsuresh.in/user.php?u=123

2) http://sforsuresh.in/user.php?u=123&cat=php

3) http://sforsuresh.in/page.php

You can just rewrite it like

1) http://sforsuresh.in/user/123

2) http://sforsuresh.in/user/123/php

3) http://sforsuresh.in/page

Let see how we can achieve this:

1) You need to enable "mod_rewrite" of apache as below:

For enabling "mod_rewrite" open file "httpd.conf" and remove "#" from beginning of the below line:

 LoadModule rewrite_module modules/mod_rewrite.so

2) Htaccess file: Create .htaccess file in your root directory, just open notepad and save the file with".htaccess" (no name only extension) as UTF-8.

If you already have one, no need to create it again. Put the below lines:

RewriteEngine On
RewriteRule ^user/([0-9]+) user.php?u=$1 [NC, L]
RewriteRule ^user/([0-9]+)/([0-9a-zA-Z]+) user.php?u=$1&cat=$2 [NC, L]
rewirteRule ^page  page.php

Let see what i have written:

RewriteEngine: To enable we need to enable rewrite engine on.

RewriteRule: The RewriteRule directive is the real rewriting of rules that need to be executed.

$1 and $2 : Are variable used to represent dynamic values which we are pass in URL against id and category.

First ([0-9]+) represent $1 and second ([0-9a-zA-Z]+) represent $2 (from left to right) and so on.

NC: Non case sensitive pattern match

L : Stop the rewriting process immediately and don't apply any more rules after this.

Few more directive you can use it as below:

RewriteBase : Specifies the URL prefix to be used. If your project inside some folder say myproject than you can write like this:

RewriteBase /myproject/

RewriteCond : defines a rule condition.

One or more RewriteCond can precede a RewriteRule directive.

You can get more information or try and test your own rules:

1) Apache.org

2) Bluehost.com




Your feedbacks are most welcome..