Mar
3
2011

Mod URL Rewrite

What is Mod URL Rewrite… A basic understanding?

Mod URL Rewrite mean to

a.) A fancy URL.

b.) A URL that is Search Engine Friendly.

C.) A URL that is user friendly too.

So, Mod URL Rewrite means to create a fancy web URL that is User and Search Engine Friendly too.

Example:

Original URL

Fancy URL

Another Example:

Original URL

Fancy URL

Modification from original URL to a Fancy URL is done by Rewrite Engine.

Pros and Cons of Using Mod URL Rewrite:

Pros:

1.) A clean and fancy url gives transparency to human readable.

2.) Clean and fancy urls are easily indexes by Search Engines.

3.)  Simple and elegant way to achieve Search Engine Optimization.

4.)  Hides our page extension and directory structure.

5.)  Safety from Hotlinks, which consumes our band width.

Cons:

1.) Emphasis on Server Load.(read further to know more)

2.) If a user wants to modify a URL to retrieve new data, the rewrite engine may stuck to the construction of custom URL due to the lack of named variables.

For example, it’s difficult to identify the date from the following URL:

In this case, the original URL was more useful,

Since the query string denotes month and day:

What is Rewrite Engine?

Rewrite Engine is software that modifies the URL appearance, this modification is called as URL Rewriting.

How Rewrite Engine Works?

Before moving towards the URL Rewriting module.

You must ensure that your server is able to handle URL Rewrite request. By default an apache installation comes with Rewrite Module installed on it, but it is disabled by default.

So to enable this you have to contact to your hosting provider.

What the hosting provider will do?

They will open the apache server configuration file httpd .conf

And remove # (comment) from the following line:

Let’s Start rewriting :

URL writing can be achieved by two popular ways.

1.)  Using . htaccess file, it is not more than a simple text file but having a very strong utility in itself. You must be familiar with this file because this allows you to set all kinds of server options. A simple example (a custom 404 Not Found Page)

Moreover, this file is interpreted each time by apache server when you made any request.

1.)  Using httpd .conf file, this file resides almost all type of server configurations. You can write your mod rewrite rule inside this file.

But you can do so when you have root access.

Which method is to choose???

Depending upon the following factor let we identify which method is best for you…

a.)  Load Issue: A critical question comes in the mind when using new things.

Consider a scenario, you have a very huge site like ebay and you want to use mod URL Rewrite in this.

Obliviously, use of . htaccess file for this purpose emphasis directly on Server Load, because as we know . htaccess file interprets every time by apache server for its each request.

Hence, you should use httpd .conf file for this purpose. As it compile once.

b.) Performance: Performance of a webpage or web portal is another big issue.

Consider another scenario,

You have 1000 of visitors a day, again . htaccess file may reduce the performance of your webportal.

Hence, you should use httpd .conf file for this also.

C.)    Easyness: Most of the time we have small and medium level WebPages that has moderate amount of visitors. And Want to promote your website on Search Engine Indexing as well.

The easiest way to do this is using . htaccess file, because this file is in our control. So we can edit it any time.

Thus, as per your requirement you can choose any of above method.

For the sake of all the users we will use . htaccess file in this tutorial.

Follow the steps:

1.) Connect to server via FTP and download the . htaccess file to you local machine from document root i.e. from public_html folder.

Important: Keep the orginal . htaccess file on some safe place, in case if we did anything wrong it may cause to our site down, so immediately we can replace this by old one.

2.) The best practice to start with Mod URL Rewrite is to download the whole site on our local machine’s WAMP/LAMP server. And edit . htaccess file.

3.) Now Open . htaccess file in your favorite text editor.

It may be possible this file contents some text or content nothing.

4.) Now you have to add following commands in your . htaccess:

a.) +FollowSymLinks is one of security feature of rewrite engine. You can’t use the rewrite module without this line

b.) Finally most important requirement is to turn the rewrite engine on.

I strongly suggest you to begin you rewriting code with following two commands:

c.) Following command explicitly set the Base URL for our rewrites.

Now Write Rewrite Rule:

Each Rewrite Rule is executed by  a command RewriteRule

Syntax to write a rule is:

RewriteRule FANCY_NAME ORIGINAL_NAME

For Example:

The existing url is :

And I want this  :

More redable.

Hence, Rewrite Rule for this would be:

Where ^ indicates that a pattern must start with extras and $ indicates that a patter must ends with extras. See this is based on Regular expressions. For better understanding of this you must read the Regular Expression basic tutorial.

Now what this Rewrite Rule will do.

When we hit the URL www.yourdomain.com/extras on our web browser. The . htaccess file matches the patter extras and execute its RewriteRule Command to call the contents of xyz.php.

It looks like, www.yourdomain.com/extras is working independently but actually it is fetching the content of xyz.php using the Rewrite Rule pattern matching.

Hence the actual URL is modified but it contents remain the same.

Thus, the final . htaccess file will look like:

Again fancy URLs are :

Last but not the least; Since this is the basic tutorial for Mod URL Rewrite, so we have to stop here.

And, the advance tutorial with some advance examples on Regular Expression and Link factory will arrive soon.

About the Author: Murtaza Malek

Team Leader at Luutaa Technologies, having experience in different web and mobile technologies. Joined Nov 17th 2010 (U turn of his life). Loves to play with JQUERY, AJAX.