Howto Beautify Ugly .PHP URL’s
January 17th, 2009 Category: Apache, PHP/MySQLYou probably know this ugly .PHP links with many parameters like
http://technitip.net/test.php?param1=p1¶m2=p2¶m3=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¶m2=p2¶m3=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¶m2=p2¶m3=p3¶m4=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¶m2=$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¶m2=$4¶m3=$6 [L,QSA,NC]
RewriteRule ^(test)/([a-z0-9]+)(/[^/]+)?/([a-z0-9]+)(/[^/]+)?/?(.*)$ test.php?param1=$2¶m2=$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!













Leave a Reply