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 :

  1. 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 :

  1. 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) :

  1. 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 :

  1. <Directory "${path}/www">
  2. #
  3. # Possible values for the Options directive are "None", "All",
  4. # or any combination of:
  5. #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
  6. #
  7. # Note that "MultiViews" must be named *explicitly* --- "Options All"
  8. # doesn't give it to you.
  9. #
  10. # The Options directive is both complicated and important.  Please see
  11. # <a href="http://httpd.apache.org/docs/2.2/mod/core.html#options<br />
  12. #" title="http://httpd.apache.org/docs/2.2/mod/core.html#options<br />
  13. #">http://httpd.apache.org/docs/2.2/mod/core.html#options<br />
  14. #</a> for more information.
  15. #
  16. Options Indexes FollowSymLinks
  17.  
  18. #
  19. # AllowOverride controls what directives may be placed in .htaccess files.
  20. # It can be "All", "None", or any combination of the keywords:
  21. #   Options FileInfo AuthConfig Limit
  22. #
  23. AllowOverride None
  24.  
  25. #
  26. # Controls who can get stuff from this server.
  27. #
  28. Order allow,deny
  29. Allow from all
  30.  
  31. </Directory>

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) :

  1. LoadModule rewrite_module modules/mod_rewrite.so
  2. AddModule mod_rewrite.c

In the same file, look for :

  1. <Directory "${path}/www">
  2.  
  3. #
  4. # This may also be "None", "All", or any combination of "Indexes",
  5. # "Includes", "FollowSymLinks", "ExecCGI", or "MultiViews".
  6. #
  7. # Note that "MultiViews" must be named *explicitly* --- "Options All"
  8. # doesn't give it to you.
  9. #
  10. Options Indexes FollowSymLinks Includes
  11.  
  12. #
  13. # This controls which options the .htaccess files in directories can
  14. # override. Can also be "All", or any combination of "Options", "FileInfo",
  15. # "AuthConfig", and "Limit"
  16. #
  17. AllowOverride All
  18.  
  19. #
  20. # Controls who can get stuff from this server.
  21. #
  22. Order allow,deny
  23. Allow from all
  24. </Directory>

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 :

  1. a2enmod rewrite

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

  1. 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.

  1. /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 :

  1. /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 :

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 :

  1. ren htaccess.txt .htacces

Rewriting rules in .htaccess file

Insert these lines in the .htaccess file :

  1.  Options +FollowSymlinks
  2.  RewriteEngine on
  3.        
  4.  # -------------------------------------------------------------------------
  5. # > URL REWRITING TEST
  6. # -------------------------------------------------------------------------</p>
  7. <p>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).

It's about

Comments

Merci beaucoup !

Merci beaucoup !

thank you very much! This

thank you very much! This great job

Thanks a lot. It worked for

Thanks a lot. It worked for me.

Thanx man. This great job

Thanx man. This great job

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <actionscript>, <apache>, <bash>, <dos>, <html>, <javascript>, <mysql>, <php>, <xml>.
  • Every instance heading tags will be modified to include an id attribute for anchor linking.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • Image links with 'rel="lightshow"' in the <a> tag will appear in a Lightbox slideshow when clicked on.
  • Links to HTML content with 'rel="lightframe"' in the <a> tag will appear in a Lightbox when clicked on.
  • Links to video content with 'rel="lightvideo"' in the <a> tag will appear in a Lightbox when clicked on.
  • Links to inline or modal content with 'rel="lightmodal"' in the <a> tag will appear in a Lightbox when clicked on.
  • Links to specified hosts will have a rel="nofollow" added to them.

  • Insert <!--tableofcontents [list: ol; title: Table of Contents; minlevel: 2; maxlevel: 3; attachments: yes;]--> to insert a mediawiki style collapsible table of contents. Arguments within [] are optional.

More information about formatting options