url rewriteing with Apache
Posted On April 25, 2008 by Sneha Philipose filed under Programming
What is mod_rewrite?
The module mod_rewrite is a really sophisticated Apache module that provides a powerful way to undertake URL manipulations. You can solve many problems using the flexible design of mod_rewrite. It is possible to redirect www.domain.com to domain.com, create virtual shortcuts for URLs and much more.
So how is this different from a classic redirection? Well, the main difference is that this "redirection" is totally transparent to the end-user; he/she will not know that the URL has been rewritten. Therefore, there is no need for any special browser or software on the visitor's end.
Enable mod_rewrite in Apache Server
Enabling mod_rewrite in Apache web server 1.X is easy because it is included in the installation and can be disabled with just a # in front of its module configuration lines in httpd.conf. So, all you have to do is enable mod_rewrite, as follows:
Open httpd.conf
Remove # from the line #LoadModule rewrite_module
Remove # from the line #AddModule mod_rewrite.c
Restart the Apache process
A mod_rewrite example
So, let's write a simple mod_rewrite example. We are just going to redirect the URL for rewrite.html to the page write.html. First, let's create the rewrite and write pages in HTML.
Check out code 1 for the rewrite’s web page.
Code 1
<html>
<head>
<title>Rewrite's webpage</title>
</head>
<body>
<p><b><font size="3" face="Verdana" color="#000080">
This is the Rewrite Web page Using Mod_rewrite</font></b>
</p>
</body>
</html>
Similarly, create the write.html page. Upload both of these to your web server and check whether you can view both of them. Now we are going to add a couple of lines to your .htaccess file.
About .htaccess file
The .htaccess file is a text file that contains Apache directives. You can control the way the Web server responds to requests to your files by creating a .htaccess file in your Web directory or one of its subdirectories. A .htaccess file contains one or more server directives — small commands known by the server. This file tells the server how to behave.
Following are a couple of specific properties you should know about .htaccess file:
1. A .htaccess file must be a plain text file;
2. A .htaccess file contains server directives, one per line;
3. A .htaccess file applies server directives to a directory as well as its subdirectories; and
4. A .htaccess file in a subdirectory overrides any .htaccess files found in its parent directories.
Each line in a .htaccess file that starts with a # symbol is ignored. Use the text editor or Notepad or SimpleText to create and modify .htaccess file. We are going to add the following line of the code in the .htaccess file.
RewriteEngine on
RewriteRule ^rewrite.html$ write.html
Note that in the above code we have used three special characters. Caret (^) signifies the start of an URL under the current directory. This directory is whatever directory the .htaccess file is in. You’ll start almost all matches with a caret. The dollar sign ($) signifies the end of the string to be matched. You should add this to stop your rules matching the first part of longer URLs. The period or dot (.) before the file extension is a special character in regular expressions and would mean something special if we didn’t escape it with the backslash, which tells Apache to treat it as a normal character.
Upload this .htaccess file to the same directory as we uploaded the rewrite.html and write.html files.
Type http://www.developeriq.com/modrewrite/rewrite.html in your URL browser. You will find the write.html page content.
If you still see the rewrite.html page contents, then check if you have followed the instructions correctly (you may have to clear your cache). If things still aren't working for you, then contact your technical support people and ask them to enable mod_rewrite in their httpd.conf file for you.
The above rule will make your server transparently redirect from rewrite.html page to write.html page.
Here is another example. Add the following code in the .htaccess file and upload:
RewriteRule /articles/([0-9]+) /articles.php?id=$91.
When you browse from the URL, it is displayed as: http://www.developeriq.com/articles/91/ instead of being displayed as http://www.developeriq.com.com/articles.php?id=91.
You don't have to limit yourself to numbers either, you can use [a-z] or [A-Z] too.
Here are some flags that you can use:
· f: send a 403 forbidden error to the browser;
· NC: make the rule case-insensitive; and
· g: send a 410 gone error to the browser.
Not only can you rewrite URLs using rules, but you can also add conditions to these rules, so they won't be executed in every case.
We can also test the condition with RewriteCond method. Here is the simple condition, which will test the browser, and depending on the browser, load the page. Add the following code in the .htaccess file:
RewriteCond %{HTTP_USER_AGENT} ^Opera.*
RewriteRule ^/$ /index_opera.php [L]
RewriteCond %{HTTP_USER_AGENT} ^Netscape.*
RewriteRule ^/$ /index_netscape.php [L]
RewriteRule ^/$ /index.php [L]
Then try executing the index page from different browsers. This will load a special page for Opera and Netscape, and load a default page otherwise. Here are some variables that you can use in your RewriteCond conditions:
HTTP_USER_AGENT (browser)
HTTP_REFERER (referring page)
HTTP_HOST (domain name - yoursite.domain.com)
TIME, TIME_YEAR, TIME_DAY, TIME_HOUR, TIME_MIN, TIME_SEC
Complex mod_rewrite example
Let's try a little more complex example now. Suppose you have a web page that takes a parameter. This parameter tells the page how it should be displayed and what content to put into it. As a humans, we cannot remember the additional syntax of query strings for URLs and neither do search engines. Both prefer a straight URL, with no extra words at the end.
In this example, we will create an index page that will take a page parameter.
So, a link like index.php?page=department would take you to a department page, while a link to index.php?page=education would take you to an education page. What we'll do with mod_rewrite is we will display page/department/ from index.php?page=department.
The power of mod_rewrite comes at the expense of complexity.
It will be with regular expressions. Using regular expressions, you can have your rules matching a set of URLs at a time and mass-redirect them to their actual pages.
We need to add the following code in our .htaccess file to accomplish that:
RewriteEngine on
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
Let's go through RewriteRule and work out exactly about:
^page/
Sees whether the requested page starts with page/. If it doesn't, this rule will be ignored.
([^/\.]+)
Here, the enclosing brackets signify that the RewriteRule will remember anything that is matched. Inside the brackets, it says "I'd like one or more characters that aren't a forward slash or a period, please". Whatever is found here will be captured and remembered.
/?$
Makes sure that the only thing that is found after what was just matched is a possible forward slash and nothing else. If anything else is found, then this RewriteRule will be ignored.
index.php?page=$1
The actual page that will be loaded by Apache! $1 is magically replaced with the text, which was captured previously.
[L]
Tells Apache to not process any more RewriteRules if this one was successful.
Let's write a quick page to test that this is working. The following test script (code 2) will simply echo the name of the page you asked for on the screen, so that you can check if the RewriteRule is working or not.
Code 2
<html>
<head>
<title>Second mod_rewrite example</html>
</head>
<body>
<p>
The requested page was:
<?php echo $_GET['page']; ?>
</p>
</body>
</html>
Again, upload both the index.php page and .htaccess file to the same directory. Then,
test it.
If you put the page in http://www.developeriq.com/modrewrite/, then try requesting http://www.developeriq.com/modrewrite/page/software. The URL in your browser window will show the name of the page that you requested, but the index.php script will create the content of the page. This technique can obviously be extended to pass multiple query strings to a page.
The module mod_rewrite is a really sophisticated Apache module that provides a powerful way to undertake URL manipulations. You can solve many problems using the flexible design of mod_rewrite. It is possible to redirect www.domain.com to domain.com, create virtual shortcuts for URLs and much more.
So how is this different from a classic redirection? Well, the main difference is that this "redirection" is totally transparent to the end-user; he/she will not know that the URL has been rewritten. Therefore, there is no need for any special browser or software on the visitor's end.
Enable mod_rewrite in Apache Server
Enabling mod_rewrite in Apache web server 1.X is easy because it is included in the installation and can be disabled with just a # in front of its module configuration lines in httpd.conf. So, all you have to do is enable mod_rewrite, as follows:
Open httpd.conf
Remove # from the line #LoadModule rewrite_module
Remove # from the line #AddModule mod_rewrite.c
Restart the Apache process
A mod_rewrite example
So, let's write a simple mod_rewrite example. We are just going to redirect the URL for rewrite.html to the page write.html. First, let's create the rewrite and write pages in HTML.
Check out code 1 for the rewrite’s web page.
Code 1
<html>
<head>
<title>Rewrite's webpage</title>
</head>
<body>
<p><b><font size="3" face="Verdana" color="#000080">
This is the Rewrite Web page Using Mod_rewrite</font></b>
</p>
</body>
</html>
Similarly, create the write.html page. Upload both of these to your web server and check whether you can view both of them. Now we are going to add a couple of lines to your .htaccess file.
About .htaccess file
The .htaccess file is a text file that contains Apache directives. You can control the way the Web server responds to requests to your files by creating a .htaccess file in your Web directory or one of its subdirectories. A .htaccess file contains one or more server directives — small commands known by the server. This file tells the server how to behave.
Following are a couple of specific properties you should know about .htaccess file:
1. A .htaccess file must be a plain text file;
2. A .htaccess file contains server directives, one per line;
3. A .htaccess file applies server directives to a directory as well as its subdirectories; and
4. A .htaccess file in a subdirectory overrides any .htaccess files found in its parent directories.
Each line in a .htaccess file that starts with a # symbol is ignored. Use the text editor or Notepad or SimpleText to create and modify .htaccess file. We are going to add the following line of the code in the .htaccess file.
RewriteEngine on
RewriteRule ^rewrite.html$ write.html
Note that in the above code we have used three special characters. Caret (^) signifies the start of an URL under the current directory. This directory is whatever directory the .htaccess file is in. You’ll start almost all matches with a caret. The dollar sign ($) signifies the end of the string to be matched. You should add this to stop your rules matching the first part of longer URLs. The period or dot (.) before the file extension is a special character in regular expressions and would mean something special if we didn’t escape it with the backslash, which tells Apache to treat it as a normal character.
Upload this .htaccess file to the same directory as we uploaded the rewrite.html and write.html files.
Type http://www.developeriq.com/modrewrite/rewrite.html in your URL browser. You will find the write.html page content.
If you still see the rewrite.html page contents, then check if you have followed the instructions correctly (you may have to clear your cache). If things still aren't working for you, then contact your technical support people and ask them to enable mod_rewrite in their httpd.conf file for you.
The above rule will make your server transparently redirect from rewrite.html page to write.html page.
Here is another example. Add the following code in the .htaccess file and upload:
RewriteRule /articles/([0-9]+) /articles.php?id=$91.
When you browse from the URL, it is displayed as: http://www.developeriq.com/articles/91/ instead of being displayed as http://www.developeriq.com.com/articles.php?id=91.
You don't have to limit yourself to numbers either, you can use [a-z] or [A-Z] too.
Here are some flags that you can use:
· f: send a 403 forbidden error to the browser;
· NC: make the rule case-insensitive; and
· g: send a 410 gone error to the browser.
Not only can you rewrite URLs using rules, but you can also add conditions to these rules, so they won't be executed in every case.
We can also test the condition with RewriteCond method. Here is the simple condition, which will test the browser, and depending on the browser, load the page. Add the following code in the .htaccess file:
RewriteCond %{HTTP_USER_AGENT} ^Opera.*
RewriteRule ^/$ /index_opera.php [L]
RewriteCond %{HTTP_USER_AGENT} ^Netscape.*
RewriteRule ^/$ /index_netscape.php [L]
RewriteRule ^/$ /index.php [L]
Then try executing the index page from different browsers. This will load a special page for Opera and Netscape, and load a default page otherwise. Here are some variables that you can use in your RewriteCond conditions:
HTTP_USER_AGENT (browser)
HTTP_REFERER (referring page)
HTTP_HOST (domain name - yoursite.domain.com)
TIME, TIME_YEAR, TIME_DAY, TIME_HOUR, TIME_MIN, TIME_SEC
Complex mod_rewrite example
Let's try a little more complex example now. Suppose you have a web page that takes a parameter. This parameter tells the page how it should be displayed and what content to put into it. As a humans, we cannot remember the additional syntax of query strings for URLs and neither do search engines. Both prefer a straight URL, with no extra words at the end.
In this example, we will create an index page that will take a page parameter.
So, a link like index.php?page=department would take you to a department page, while a link to index.php?page=education would take you to an education page. What we'll do with mod_rewrite is we will display page/department/ from index.php?page=department.
The power of mod_rewrite comes at the expense of complexity.
It will be with regular expressions. Using regular expressions, you can have your rules matching a set of URLs at a time and mass-redirect them to their actual pages.
We need to add the following code in our .htaccess file to accomplish that:
RewriteEngine on
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
Let's go through RewriteRule and work out exactly about:
^page/
Sees whether the requested page starts with page/. If it doesn't, this rule will be ignored.
([^/\.]+)
Here, the enclosing brackets signify that the RewriteRule will remember anything that is matched. Inside the brackets, it says "I'd like one or more characters that aren't a forward slash or a period, please". Whatever is found here will be captured and remembered.
/?$
Makes sure that the only thing that is found after what was just matched is a possible forward slash and nothing else. If anything else is found, then this RewriteRule will be ignored.
index.php?page=$1
The actual page that will be loaded by Apache! $1 is magically replaced with the text, which was captured previously.
[L]
Tells Apache to not process any more RewriteRules if this one was successful.
Let's write a quick page to test that this is working. The following test script (code 2) will simply echo the name of the page you asked for on the screen, so that you can check if the RewriteRule is working or not.
Code 2
<html>
<head>
<title>Second mod_rewrite example</html>
</head>
<body>
<p>
The requested page was:
<?php echo $_GET['page']; ?>
</p>
</body>
</html>
Again, upload both the index.php page and .htaccess file to the same directory. Then,
test it.
If you put the page in http://www.developeriq.com/modrewrite/, then try requesting http://www.developeriq.com/modrewrite/page/software. The URL in your browser window will show the name of the page that you requested, but the index.php script will create the content of the page. This technique can obviously be extended to pass multiple query strings to a page.

Test commented, on June 18, 2008 at 11:30 p.m.:
??????? ,
????? ???? ???? ?????????? ???? ????? ?????????????? , ?????? ??????? ?????? ???????? ???? ???????? ?????????? ???? ???????????? ???? ?????? ???????? ???? ??????????? , ??? ???? ????? ???????????? ?????????? ???? ??? ???????? ????? ???????? ???????? ????? ????? ? ??????? ??????????? ???? ?????? ??????????????????? ??????????????????.