For example, you might be working on a website in PHP with the following URL structure:
This structure isn’t ideal, because it is a dynamic URL. You could solve this problem by changing the URL to the following:
If you try both URLs you will see that it shows the same content. However, they are two distinct pages. The second URL simulates a static page, which is converted behind the curtains to call the real page. There is no forum-3.html file on the server.
Here are the steps to achieve this with mod_rewrite.
An elegant solution
When you are creating a new software application, it is important to be aware of all of the different tools at your disposal. You can reduce your budget for software development by using the most efficient coding practices, such as relying on the mod_rewrite method.
This is the simplest approach with the mod_rewrite method. To achieve this, you must have the following:
- An Apache server version 1.2 or better.
- Access to edit .htaccess configuration files and http.conf if necessary.
If you are used to using Apache servers, you have probably heard about Apache’s mod_rewrite module. This module comes by default in most Apache versions, but on *nix systems it may have been compiled without this module.
In order to use this module, we have to activate it first. In case you don’t have the module activated you will have to edit your http.conf file and remove the comment from the line that loads the module.
It is important to understand exactly what this module does.
The mod_rewrite is executed after making a request on your server and before executing any script. This module applies a “filter” configured by one over the URLs and rewrites them behind the curtains.
For example, you could have a hypothetical URL like the following:
Then the mod_rewrite method could convert it to:
The conversion of the URL is done on the server end, so the user will not be able to see any of this. However, if you see the result that comes from the execution of the URL “real”.
It is important to understand that mod_rewrite cannot be used to change the URL the user sees is the Address bar of his browser unless an external redirection is invoked. However, an external address finally exposes the dynamic URL, so mod_rewrite does an internal redirection.
It is also important to understand that mod_rewrite changes the address of the file and the variables of the requested URL. The module does not change the printed variables at any time.
Putting mod_rewrite into practice
Here is a practical approach to using the mod_rewrite method.
Let’s say you have a website that provides reverse searches based on various variables. You need to have an application that is called in the following way:
The URL is not well organized, so we want our users to be able to access it in the following way:
As you can see, it’s more intuitive and friendly.
Using mod_rewrite, you don’t need to make any changes to your application. You don’t even need to create separate directories or an additional file called integervalue.htm.
What we are going to do is that when a user makes a request of “http://domain.com/reverse-search/identity/integervalue.htm”, we will use the mod_rewrite to filter and convert the URL to http://domain.com/cgi-bin/reverse-search-application.cgi?type=identify&integervalue=1&timeconstant=3. Again, this all happens on the server side, using the internal redirection as we said.
Since we are sure that we have the mod_rewrite installed and active, we are going to create a new .htaccess configuration file.
Inside our .htaccess we are going to initialize the mod_rewrite module:
Since we have this, we have to configure the rules or filters we are going to use. Each one will have to go on a new line and we can have as many as we want and need.
Then we will create our filter that will look like this:
RewriteRule ^ http://domain.com/cgi-bin/reverse-search-application.cgi?type=identify&integervalue=1&timeconstant=3”, [L,NC]
Let’s understand our filter line. The filter consists of 4 elements, each separated by a blank.
The first element is:
At this stage, we must indicate that we are creating a new rule or filter for the mod_rewrite machine. The second element is:
In this case, it’s the URL we’re looking for. It is important to keep in mind that we must always use the relative directory, that is, we must not put the domain of our site, the mod_rewrite will put it for us.
The third part is the new URL to which we want to redirect, also the address must be relative, mod_rewrite will put for us the domain of our site.
NOTE: We cannot use mod_rewrite to make an internal redirection to a URL that is not within our domain.
Mod_rewrite is an important method to understand. Use this guide to get you smoothly through it.