PHP Lesson 1: Pretty URLs!

Posted by

I am going to start a new practice here. Every now and again I’m going to post some PHP code with some explanation. Today, I’m going to write about what I’ve been calling “pretty URLs” and how to create and manage them in PHP.

PHP includes a variable in the superglobal scope $_SERVER called “PATH_INFO.” PATH_INFO includes information entered after the name of the requested script.

Let’s use as an example. The URL of a story is constructed as such:[id]/[url_friendly_title].html

The story is also accessible as /read.php?id=[id]

So how do we construct this so-called “pretty URL?” Using PATH_INFO. Read on for details.

Knowing the URL, we will use some code like this:

if(isset($_SERVER['PATH_INFO'])) {
	$getvars = explode('/', $_SERVER['PATH_INFO']);
	list($null,$id,$title) = $getvars;
	$id = (int) $id;
} else {
	$id = (int) $_GET['id'];

Let’s review piece by piece.

First, we find out if there is PATH_INFO set. This is information BEFORE a question mark, so it doesn’t include GET variables. Then, we use the standard explode() function to break up the PATH_INFO at the “/” sign, typical in a URL. It fills the “getvars” array, which I’ve arbitrarily named, it could be anything.

Now, the first element is always NULL in this sense, but the second element is the id we had before. And the third element is the title, which is something like title_of_the_post.html. In another post, I’ll discuss sanitizing your title.

So what have you discovered is that the “title.html” portion is useless. It’s there solely to please search engines and users who care. Try it. All that matters is the number. There are reasons why you’d validate the title, for example, so people don’t link to and have it be a valid URL. But for now, let’s keep it simple.

Next is just to construct a linked URL that you like. Perhaps your database tracks date. You could make a URL Then do a little magic:

if(isset($_SERVER['PATH_INFO'])) {
	$getvars = explode('/', $_SERVER['PATH_INFO']);
	list($null,$year,$month,$day,$time) = $getvars;
	if(strstr($time,".html")) { $time = str_replace(".html","",$time); }
} else {
	$id = (int) $_GET['id'];

Then, after you fix the time, you can assemble the timestamp for SELECTing:

Let’s say you prefer the WordPress format, which is Create an extra field in the database called “title” and record the title as you generate your entry. It becomes a permalink, uneditable, even if the title changes. Or not. That’s up to you.

Either way, using PATH_INFO truly puts you in control of the URLs on your website.

One comment

  1. Hi, good post.
    Now i have a little question: where do you put this part of code? I know the purpose of this code is to get variable $id, so do you just put it in front of the switch($id) or sort of things? In php template engine smarty, every url request is redirect to index.php. Therefore I guess this code of prasing $_SERVER[‘PATH_INFO’] should be put in index.php. The truth is some projects i’ve fond online is not like this. I didn’t find sort of code. Any ideas?

Leave a Reply

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