Howto Beautify Ugly .PHP URL’s

January 17th, 2009 Category: Apache, PHP/MySQL

You probably know this ugly .PHP links with many parameters like

http://technitip.net/test.php?param1=p1&param2=p2&param3=p3


I really don’t like this look. Doesn’t the following URL look much better?

http://technitip.net/test/p1/p2/p3


I think yes. So here is tutorial how you can beautify your links using Apache and mod_rewrite.

The Test Script

We assume that mod_rewrite is loaded in your Apache config and generate a simple PHP script called “test.php”:

<?php
echo "Parameter 1: " . $_REQUEST['param1'] . "<br />";
echo "Parameter 2: " . $_REQUEST['param2'] . "<br />";
echo "Parameter 3: " . $_REQUEST['param3'] . "<br />";
?>

This script gets the parameters param1, param2, etc. which have been given to the PHP page and prints them out. To check it we put the URL into the browser:

http://technitip.net/test.php?param1=p1&param2=p2&param3=p3

And get the result:

Parameter 1: p1
Parameter 2: p2
Parameter 3: p3

Simple Rewrite

Fine, passing parameters to our PHP script is working but still looking ugly. Now we generate a .htaccess file in the root directory of your web directory:

RewriteEngine On
RewriteBase /
RewriteRule ^(test)/?(.*)$ test.php [L,QSA,NC]

What will happen? test.php will be redirected to test and all parameters will be parsed like before, we can test it:

http://technitip.net/test?param1=p1&param2=p2&param3=p3&param4=p4

First Parameter Rewrite

Nice, but not yet what we really want. So we put another line into the .htaccess (before our first RewriteRule, not after! It’s important):

RewriteRule ^(test)/([a-z0-9]+)(/[^/]+)?/?(.*)$ test.php?param1=$2 [L,QSA,NC]
RewriteRule ^(test)/?(.*)$ test.php [L,QSA,NC]

For every parameter we need a new expression /([a-z0-9]+)(/[^/]+)?/

This will convert the line /test/p1 into test.php?param1=p1. Note that only the characters a-z und 0-9 are allowed as parameters. For the first parameter $2 is used.

Second Parameter

We repeat this step for every further parameter we need to hand over, $4 is used for the second parameter (not $3!):

RewriteRule ^(test)/([a-z0-9]+)(/[^/]+)?/([a-z0-9]+)(/[^/]+)?/?(.*)$ test.php?param1=$2&param2=$4 [L,QSA,NC]
RewriteRule ^(test)/([a-z0-9]+)(/[^/]+)?/?(.*)$ test.php?param1=$2 [L,QSA,NC]
RewriteRule ^(test)/?(.*)$ test.php [L,QSA,NC]

Three Parameters

Or with 3 parameters. Note: Since the .htaccess is parsed from top to bottom the rewrite rule with the highest number of parameters must be located at the top of the .htaccess. $6 is used for the third parameter!

RewriteRule ^(test)/([a-z0-9]+)(/[^/]+)?/([a-z0-9]+)(/[^/]+)?/([a-z0-9]+)(/[^/]+)?/?(.*)$ test.php?param1=$2&param2=$4&param3=$6 [L,QSA,NC]
RewriteRule ^(test)/([a-z0-9]+)(/[^/]+)?/([a-z0-9]+)(/[^/]+)?/?(.*)$ test.php?param1=$2&param2=$4 [L,QSA,NC]
RewriteRule ^(test)/([a-z0-9]+)(/[^/]+)?/?(.*)$ test.php?param1=$2 [L,QSA,NC]
RewriteRule ^(test)/?(.*)$ test.php [L,QSA,NC]

We test again with 3 parameters:

http://technitip.net/test/p1/p2/p3


Parameter 1: p1
Parameter 2: p2
Parameter 3: p3

Looks better, indeed.

Hint

Be sure you have enabled “Options FollowSymLinks” or Options “SymLinksIfOwnerMatch” in your Apache or virtual host config. Otherwise mod rewrite will not work!

Share and Enjoy:
These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Reddit
  • Webnews
  • MisterWong
  • Y!GG
  • Facebook
  • Furl
  • Google Bookmarks
  • Live-MSN
  • Readster
  • YahooMyWeb

Related posts:

  1. PHP Accelerator
  2. Simple PHP Flood Protection Class
  3. Domain Redirects
  4. Howto Restart Apache Graceful
This entry was posted on Saturday, January 17th, 2009 and is filed under Apache, PHP/MySQL. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply