Friday, January 7, 2011

Search (seo) Friendly Url - apache mod_rewrite

For rewriting the URL, you should create a .htaccess file in the root folder of your web directory

at the beginning of url rewritting

These first two lines required whenever you use mod_rewrite  (the part of Apache that does all this magic) will always be the same:you only need to do this once per .htaccess file:

Options +FollowSymlinks 
RewriteEngine on

 before any ReWrite rules. note: +FollowSymLinks must be enabled ( RewriteEngine on ) for any rules to work, this is a security requirement of the rewrite engine.

for an example 
the Dynamic url is:  http://localhost/mathu/index.php?view=categorise
 Create file called .htaccess in the 'localhost/mathu' directory. Place the following mod_rewrite code in it.

Options +FollowSymLinks
RewriteEngine on
RewriteRule index/view/(.*)/ index.php?view=$1
RewriteRule index/view/(.*) index.php?view=$1 

After that, the generated seo friendly  Url look like this:
http://localhost/mathu/index/view/categorise/ 

 Another Example
http://localhost/mathu/index.php?view=categorise&category_name=abc
mod_rewrite code:

Options +FollowSymLinks
RewriteEngine on
RewriteRule index/view/(.*)/category_name/(.*)/ index.php?view=$1&category_name=$2
RewriteRule index/view/(.*)/category_name/(.*) index.php?view=$1&category_name=$2               
            

After that, the generated seo friendly  Url look like this:
 http://localhost/mathu/index/view/categorise/category_name/abc/ 

No comments:

Post a Comment