mod_rewrite is an Apache module which provides a rule-based rewriting engine to rewrite requested URLs on the fly.It supports an unlimited number of rules and an unlimited number of attached rule conditions for each rule to provide a really flexible and powerful URL manipulation mechanism.

Why URL Rewriting?

SEO(Search engine optimization) – Static URLs typically Rank better in Search Engines than the dynamic ones.

Dynamic URLs vs. Static URLs From Google

User-friendlier – Static URLs are always more friendlier than dynamic URL’s.

Security – mod_rewrite helps you hide the parameters passed in the application.

How to use mod_rewrite to rewrite URL’s?

mod_rewrite is really powerful if you are familiar with the regular expressions which it uses. But learning the whole pattern syntax can be quite complicated, especially for the non-technical user.

Exmpale:

First create a file called .htaccess and place it in folder where you want the rewriting to take effect. In case already .htaccess file exists, you can simply add the lines to it.

Open it in a your favourite text editor and start with:

Options +FollowSymLinks
RewriteEngine on

Above line makes the rewrite engine to switch on. You can now add as many rewrite rules as you want.

The format is simple:

RewriteRule rewrite_from rewrite_to

“RewriteRule” is static text, i.e. you should not change. “rewrite_from” is the address which will be typed in the browser and “rewrite_to” – which page the server will actually activate. Both of these can contain “masks”, but in “rewrite_to” we will only use $ and will discuss more or “rewrite_from” part.

To rewrite urls like index.php?task=categories to categories.html

RewriteRule ^(.*).html index.php?task=$1 //This will rewrite your urls from index.php?task=$1 to categories.html.

Meaning of regxp characters used above -

- ^ character marks the beginning. i.e. you tell the server that it should not expect anything before it.

- (.*) – This combination is the most often used and it means literally “everything”. So everything you type before “.html”

- $1 – This is a parametter, saying where the first mask should be put. If you have more than one masks you can use $2, $3 etc. You’ll see more in the following examples.

- Using “numbers”. You can easily limit the rewriter to rewrite if it meets only numbers at a certain place:

Complete example:

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^(category|product|services).html index.php?task=$1
#Here ‘|’ means the ‘OR’. This tells the server to rewrite only if the file name is category.html OR product.html OR services.html

RewriteRule ^category-([0-9]*).html index.php?task=category&id=$1
#With ([0-9]*) mask you tell the rewrite engine that on the mask place it should expect only numbers.

Note : Before rewriting urls, you have to remove any special characters from the text and its better to replace space with “-”.

References:
MOD_REWRITE for Dummies
How to: URL re-writing in PHP ?
http://www.workingwith.me.uk/articles/scripting/mod_rewrite
Pretty URLs – a guide to URL rewriting
Hide .php extension with url rewriting using .htaccess

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Reddit Post to StumbleUpon

Share or Bookmark This Post With :

9 Comments
Mayank Posted On June 15th, 2008

Thanks a lot for sharing this with all. It is really useful.

baba Posted On August 17th, 2008

really nice job……..thanxxx dear.keep it up

Bhonindra Singh Posted On August 23rd, 2008

It’s an amazing stuff, really useful for a newbie. Thanks a lot.

name Posted On September 1st, 2008

Hi!,

Neeraj Posted On September 19th, 2008

Thanks Dear………..

[...] URL REWRITE MOD_REWRITE IN PHP JavaTechie [...]

High SEO Ranking Posted On January 4th, 2010

Dude, great post informing about SEO. This is definitely better than anything I’ve ever seen.

high seo ranking Posted On January 4th, 2010

This is great in terms of search engine optimization. Nothing looks to rag upon it compared to that!

How To Rewrite An Article Posted On January 28th, 2010

Been following your blog for a while, I’ve never commented on anything. I just want to give Kudo’s for a great site and good posts…Thanks

Leave a Reply

Subscribe to RSS Feeds Follow me on Twitter Add to Favourite
 
Sponsors
 
Tags
 
Network [+]
 
© Copyright 2009 - 2011 TechiePark.com. All Rights Reserved | Powered by WordPress
This work is licensed under a Creative Commons Attribution 3.0 Unported License
Php5 powered    Mysql powered    Apache powered   Best viewed with  Spread Firefox Affiliate Button