Luutaa Technologies - Developer Blog

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

www.yourwebsite.com/home.php

Fancy URL

www.yourwebsite.com/myhome

Another Example:

Original URL

www.yourwebsite.com/post.php?year=2011&month=3

Fancy URL

www.yourwebsite.com/post/2011/3

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:

http://www.yourdomain.com/post/12/10/2006/

In this case, the original URL was more useful,

Since the query string denotes month and day:

http://www. yourdomain.com/post.php/ year=2006&month=12&day=10

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:

# LoadModule rewrite_module modules/mod_rewrite.so

Save the httpd .conf file and restart the apache server.
Congrats! Now you are able to use Mod URL Rewrite Engine.

 

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

         Options +FollowSymLinks

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

         RewriteEngine On

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

          Options +FollowSymLinks

          RewriteEngine On

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

          RewriteBase /

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 :

www.yourdomain.com/xyz.php

And I want this  :

www.yourdomain.com/extras

More redable.

Hence, Rewrite Rule for this would be:

RewriteRule     ^extras$      xyz.php

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:

Options +FollowSymLinks

RewriteEngine On

RewriteBase /

RewriteRule     ^extras$      xyz.php

RewriteRule     ^about-us$     about.php

RewriteRule     ^home$     index.php

Again fancy URLs are :

http://www.yourdomain.com/home
http://www.yourdomain.com/extras
http://www.yourdomain.com/about-us

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.

Leave a Reply:

Your email address will not be published. Required fields are marked *