The purpose of writing this tip is that somedays before I was almost lost in my current project. It was miserable for me to find out how and where a particular event is fired to execute a particular functionality though it was understandable that the event was fired because whenever I changed something in the event-handler of that particular event the changes were reflecting in the application. Then after a lot of searching and researching I found out a new thing (for me) called “URL Rewriting” as the cause of this happening. Now, lets discuss briefly about the concept “URL Rewriting”.
WHAT IS “URL Rewriting” AND WHY TO USE IT :
The URL Rewriting rule is one the best concept we can use for increasing the searching capability and usability of the developed websites by us in the search engines.
Ex: We have created an application in ColdFusion which contains the product searching feature. So whenever we like to search some product like ‘pencil’ or anything, a particular ColdFusion service is called or an event is fired to call that service to give us all the data regarding the search resullts of pencil from the database. So, our URL in the web browser for that functionality will be almost like: ( in general )
Then, from the URL also we can say that we are searching for pencils and its not at all difficult to remember the URL. And also we cannot say by seeing the URL that which technology we have used for the application which is good.
Now, the thing is how our server will know what to process when it gets the request through the last URL of the above so that it can return the user the actual data ( here the results of searching ‘pencil’ ).
So, the technique of translating the URLs like the above last one internally which the server can understand to process the particular event or service for that functionality ( here the ‘pencil’ searching ).
HOW TO DO “URL Rewriting” :
To use URL Rewriting we need to first create a file called ‘.htaccess.txt’ or ‘rules.htaccess.txt’ to contain all the rules to change or translate the URLs and put it under the root folder of the server in our system. If previously the root directory contains this file, then we should edit it instead of overwrite it.
RULE :
RewriteRule ^products/([A-Za-z0-9-]+)/?$ /search_product.cfm&product=$1 [QSA,L] # Handle requests for “pencil”
The above rule will translate the URL like :
http://www.productsearch.com/products/pencil/
to the translated URL internally for the server to understand and for executing a particular event or service to get the results :
http://www.productsearch.com/search_product.cfm&product=pencil
There are also a lots of rules for translating the URLs. I will provide some links for the tutorials of these rules at the end of this tip.
But depending on the software our server is running we will have the access of these URL Rewriting modules. In Apache server we can have the easiest way to run the URL Rewriting because the built-in URL Rewriting module of this server.
In IIS7, we can have its ownMicrosoft URL Rewrite Module 2.0 for this purpose while in the earlier versions of the IIS, we should install some add-ons like “ISAPI_Rewrite ” to rewrite the URLs.
Now, I am giving the content of the entire .htaccess.txt file :
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^products/([A-Za-z0-9-]+)/?$ /search_product.cfm&product=$1 [QSA,L] # Handle requests for “pencil”
Above, the first line is for telling the server to turn on the rewrite engine and the second line is the rule of the URL rewriting. We should write the first line only once per a .htaccess file.
The link to follow for the vast tutorial on URL Rewriting:
http://coding.smashingmagazine.com/2011/11/02/introduction-to-url-rewriting/