URL Rewriting for PHP Web Applications Using Apache mod_rewrite extension


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

Related Posts:

9 Responses


Mayank June 14, 2008 at 6:47 am

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


baba June 14, 2008 at 6:47 am

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


Bhonindra Singh June 14, 2008 at 6:47 am

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


name June 14, 2008 at 6:47 am

Hi!,


Neeraj June 14, 2008 at 6:47 am

Thanks Dear………..


Url Htaccess Re-write – Hot Scripts Forums June 14, 2008 at 6:47 am

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


High SEO Ranking June 14, 2008 at 6:47 am

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


high seo ranking June 14, 2008 at 6:47 am

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


How To Rewrite An Article June 14, 2008 at 6:47 am

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


Comments are closed.