Search Engine Friendly URLs

Search Engine Friendly URLs with PATH INFO


This method is the easiest way to do things and gives you URLs that look like this: You see Apache has a "look back" feature that lets it scan back down the URL if it doesn't find what it is looking for. In this case there is no directory or file called 12, so it looks at 999. There is no directory or file called 999 so Apache looks down the URL and sees article.php, which does exist, and calls up that script. Apache also has a global variable called $PATH_INFO that is created on every HTTP request. What this variable contains is the script being called and everything to the right of that information in the URL. So in the example we have been using $PATH_INFO will contain article.php/999/12.

So, you wonder, how do I query my database using article.php/999/12? Well first you have to split it into variables you can use, we do that using PHP's explode function.

$var_array = explode("/",$PATH_INFO);

Explode tokenizes a string (separates it) based on a delimiter. In this case we're using the delimiter "/" which will split the URL into three parts below accessed by the array $var_array;

$var_array[0] //article.php
$var_array[1] //999
$var_array[2] //12

So you can rename $var_array[1] as $article and $var_array[2] as $page_num and query your database as if the URL had originally been article.php?article=999&page_num=12.


If this method does not work for you it could mean that you are running Apache 2.X instead of Apache 1.X. Apache 2.X turns PATH_INFO off by default. To get around this you need add AcceptPathInfo On to your .htaccess file in your site's root HTML directory.

If this method still does not work it could be because PHP is being used via CGI instead of as an Apache module. There is a known bug with some versions of PHP that makes PATH_INFO not work when PHP is installed as CGI. To fix this you can either try upgrading (I believe this is fixed in newer versions) your php installation, or recompiling Apache with PHP as a module.


There was previously one major drawback to using this method. Google, and perhaps other search engines, would not index pages set up in this manner as they thought they were seeing a malformed URL. I contacted a software developer at Google and made them aware of the problem and I am happy to announce that it is now fixed. That was in 2002 as I recall and I've heard no other reports of drawbacks since. Of course, obviously with this method you do not get the benefit of hiding the extension that I mentioned in this article's introduction. For that you need the next method, ForceType.