Use of the mod-rewrite module from Apache

The URL Rewriting is a method of URLs rewriting that became essential for URLs simplification, particularly during the referencing of dynamic pages.

Installation

The main purpose of the URL Rewriting is to avoid having a coupling between the URL format and technical resources present on the server, as well as secondary gains the ability to improve the indexation of web browsers and to present less technical URLs.
The mod_rewrite module of Apache is usually present during the Apache installation :

The module can be activated thanks to the commande a2enmod :

Now we are going to indice to the Apache server to use the module by changing RewriteEngine :

Add this piece of configuration at the end of the file and safeguard :

Restart Apache for the update to take into account our configuration’s shift :


Configuration

In order to test the module’s proper functioning, we create a file PHP :

We do it again with the file .htaccess :

Your page http://www.domain.com/rewrite.php is now available via URL http://www.domain.com/rewrite.html

Activation of the URL rewriting.

Definition of the rewriting rule.

Regex of rewriting.

Searched string.

Parameter Last to indicate that it is the last rule to deal with.


Definition of rules

This time, we are going to see how to rewrite dynamic URLs composed of several parameters.
Let’s try to rewrite the following URLs :

http://www.domain.com/catalogue.php?page=cart&action=validate -> http://www.domain.com/cart.validate.html
http://www.domain.com/image.php?album=26&id=15 -> http://www.domain.com/image/26/15


Conditional rules

The conditional rewriting of URLs allows to filter and protect the access to a part of your web site or to ban someone trying to access it.
A conditional rule is declined under the following format :

Other examples :

Rules can be dynamic by using the following contextual variables :

Server’s domain.

the page (if there are information) where the visitor comes from.

Web browser and operating system, …

Required page in resquest HTTP/HTTPS.

IP address making the request.


Use of the mod-rewrite module from Apache
Vote