URL Rewriting with EasyPHP and Apache2

URL Rewriting allows a dynamic website to have clean and relevant URL addresses.

In the next article, we’ll see what is URL rewriting for, and how to put it in function on your local apache server.


Why to rewrite URLs?

Normally, a URL address of a dynamic website would look like this :

http://blog5643.com/index.php?section=1&cat=5&article=12&p=2

But if we give the server a rewriting rule, it may look like this instead :

http://blog5643.com/1-Technologie/5-Web/12-Comment-faire-son-propre-blog/page-2

That’s much cleaner, and easier to understand for the user and for Google. are a good way to improve SEO; moreover, Google doesn’t like that much URLs that have more than two parameters into its query string (like in the first example).

How to set up URL Rewriting on my local apache server?

If you wish to use URL rewriting « at home », you will probably need to activate the rewriting module (called mod_rewrite) of Apache. It is not very complex to do, but if you don’t know how, it may be frustrating.

Then, here are solutions to set up URL Rewriting on your local server, using EasyPHP or Apache 2. Note that if you use MAMP or MAMP PRO, mod_rewrite is already enabled.

The only difference between the activation of mod_rewrite for EasyPHP 1.8 and for EasyPHP 2, is that in the version 2, there’s only a line to uncomment.

Activation of mod_rewrite on EasyPHP 2

Uncomment this line (remove #) in EasyPHP’s configuration, httpd.conf (Menu e/Configuration/Apache in EasyPHP) :


LoadModule rewrite_module modules/mod_rewrite.so

Then, in the same configuration file, look for the <Directory> tag, relevant to your website. It should look like this :



#
# Possible values for the Options directive are “None”, “All”,
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that “MultiViews” must be named *explicitly* — “Options All”
# doesn’t give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be “All”, “None”, or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None

#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all


You see « AllowOverride None »? Change it for « AllowOverride All ». This little trick will say to the server to pay attention to the .htaccess file. Without this modification, the .htaccess will be ignored by the server.

Activation of mod_rewrite on EasyPHP 1.8

Uncomment the two following lines (remove #) in EasyPHP’s configuration, httpd.conf (Menu e/Configuration/Apache in EasyPHP) :


LoadModule rewrite_module modules/mod_rewrite.so
AddModule mod_rewrite.c

In the same file, look for :


#
# This may also be “None”, “All”, or any combination of “Indexes”,
# “Includes”, “FollowSymLinks”, “ExecCGI”, or “MultiViews”.
#
# Note that “MultiViews” must be named *explicitly* — “Options All”
# doesn’t give it to you.
#
Options Indexes FollowSymLinks Includes

#
# This controls which options the .htaccess files in directories can
# override. Can also be “All”, or any combination of “Options”, “FileInfo”,
# “AuthConfig”, and “Limit”
#
AllowOverride All

#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all

Take care that AllowOverride All is set to ALL and not set to NONE.

In EasyPHP 1.8, it’s already set to ALL, but in EasyPHP 2.0, you must do it by hand. Be aware that if it’s not set to ALL, the .htaccess file won’t be considered by the apache server, it’s very important (and very simple)!

Activation of mod_rewrite on Apache 2 (dedicated server)

It’s very easy to do. Run the console (for Macs, it’s called Terminal, and it’s located in System applications) and type this :


a2enmod rewrite

Then, you must edit Apache’s configuration, for that it doesn’t ignore the .htaccess file. Open configuration file :


sudo nano /etc/apache2/apache2.conf

Find the tag <Directory> that is for your website, then search for AllowOverride None, and set it to AllowOverride All.


/etc/apache2/sites-enabled/

A default file may be there, in that case, its name would be 000-default.

Once you saved your changes, restart the server :


/etc/init.d/apache2 reload

How can I verify if URL Rewriting is really activated

Check if mod_rewrite is there and activated

Very simple. Type :

phpinfo();

in a PHP file, then check that « mod_write » is there. If it’s not there, there’s something wrong. Of course, it means that rewriting doesn’t work. Take a look to  previous steps again, you may have missed something.

Create your .htaccess file (the name of the file starts with a dot), and place it at the root of your site.

How to name a .htaccess file correctly, with its dot

With Windows, it may cause a problem because this syntax is for hidden files (normally). It is possible to create the .htaccess file by using Notepad. Save your file this way : ".htaccess" (yes, with quotes).

You can also rename it in DOS, if you saved it in text format :


ren htaccess.txt .htacces

Rewriting rules in .htaccess file

Insert these lines in the .htaccess file :

Options +FollowSymlinks
RewriteEngine on

# ————————————————————————-
# > URL REWRITING TEST
# ————————————————————————-

RewriteRule ^try.html$ page_test.html [NC,L]

Create a file named page_test.html, also at the root of the website, with .htaccess.

Now that the rewriting rule is there, try to open essai.html in your browser.

  1. If you get page_test.html : URL rewriting is working!
  2. If you get an 404 error because of page_test.html : It works but page_test.html doesn’t exist.
  3. If you get an 404 error bacuse of try.html, and mod_rewrite is there when you try phpinfo() : Your rewriting doesn’t work! Are you sure you did all right?
  4. If you get an 500 error, and you are on local server : Your .htaccess file contains errors in its rewriting rules (but your rewriting is working, you must only have a look on your code and fix what’s wrong).

Ce message est également disponible en : French

4 thoughts on “URL Rewriting with EasyPHP and Apache2

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>