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:
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” 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
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
Share or Bookmark This Post With :
|
|
Posted On June 15th, 2008
Thanks a lot for sharing this with all. It is really useful. |
|
|
Posted On August 17th, 2008
really nice job……..thanxxx dear.keep it up |
|
|
Posted On August 23rd, 2008
It’s an amazing stuff, really useful for a newbie. Thanks a lot. |
|
|
Posted On September 1st, 2008
Hi!, |
|
|
Posted On September 19th, 2008
Thanks Dear……….. |
|
Posted On June 20th, 2009
[...] URL REWRITE MOD_REWRITE IN PHP JavaTechie [...] |
|
|
Posted On January 4th, 2010
Dude, great post informing about SEO. This is definitely better than anything I’ve ever seen. |
|
|
Posted On January 4th, 2010
This is great in terms of search engine optimization. Nothing looks to rag upon it compared to that! |
|
|
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
- Roundcube – Free browser-based IMAP client
- mtop – MySQL terminal based query monitor
- Kohana – Swift, Secure, and Small PHP 5 Fram...
- FormMail – free form processing PHP script
- Xataface – The fastest way to build a front-...
- dotProject – the Open Source Project Managem...
- The jQuery Form Plugin
- DocVerse has officially been acquired by Google
- HDGraph – a free tool for Windows to draw mu...
- RandomClass jQuery Plugin
- Send emails using PHPMailer an...
- Firefox Addons Essential for S...
- Free SEO Tools From SEOMoz.org...
- URL Rewriting for PHP Web Appl...
- Multiple Instances of Tomcat w...
- Apache 2.x + Tomcat 4.x + Load...
- Pagination in JSP/Java
- PHP – Mysql Open Source ...
- java.lang.OutOfMemoryError: Ja...
- HOW TO Subversion+Apache on Fe...




