Galactic Civilizations 3
Rewrite URLs in php

Rewrite URLs in php

Category: Php.
Posted by on the 06/12/2013.
Last update on the 06/12/2013.

   Next tutorial>>


This tutorial explains how to configure your apache2 server by using an .htaccess in order to rewrite URLs.


This tutorial is intended for people under Linux that use apache2 server as webserver. In this tutorial we consider that the root folder of your website is /var/www/tutorielsenfolie. Furthermore, all commands in this tutorial are executed as root. (su command).


The URL rewriting consists to put in place a system in order to have a specific URL for each generated page on the web server. For example, when a user want to access to the page, the server must return exactly the same thing as if he had requested access to the page: (If the user doesn't rewrite the URL).

The correspondence between these two URL schemes is using regular expression to retrieve the values ​​of different variables.

Apache2 server configuration

First of all, we will configure the web server in order to use the URL rewriting. To do that, we will activate the rewriting module with the following command:

a2enmod rewrite

(To deactivate the rewriting, you need to execute the following command: a2dismod rewrite).

Now, that the rewriting module is deactivated, we will allow the root folder to use the URL rewriting mechanism. To do that, execute the following command:

nano /etc/apache2/apache2.conf

Add now, the lines below at the end of the file.


<Directory /var/www/tutorielsenfolie>
AllowOverride All

The above lines allow rewriting the URL in the root folder of your website.

Testing and implementation of road

Now, we will test if the rewriting works fine. To do that, create a file .htaccess. To do that, execute the following command:

nano /var/www/tutorielsenfolie/.htaccess


RewriteEngine on
RewriteRule ^(.*)$ routur.php [L]

Then create the file router.php. To do that, execute the following command:

nano /var/www/tutorielsenfolie/ routur.php


echo 'OK';

Now we will test the URL rewriting. Go to « http://localhost/tutorielsenfolie/test.html » on your browser. If the redirection works fine, you will see the message OK. If it is not the case, the server is not (or not well) configured.

Réécriture d'url test

You like this tutorial ?
Like this website in order to promote it on facebook !

   Next tutorial>>

You like this tutorial ? Share it with your friends !
Partager sur Facebook Partager sur Twitter Partager sur Myspace Partager sur Stumbleupon Soumettre sur Reddit Partager sur Digg Ajouter à vos favoris Technorati Ajouter à vos favoris Live Ajouter à vos favoris Google Ajouter sur vos favoris Yahoo