PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Creating Search Engine Friendly URLs In PHP

Introduction

Note: This article was updated in February 2008 to include the "Using mod_rewrite as a 404 Handler" page, since using ErrorDocument in Apache results in POST data being unavailable.

One of the major reasons for using a server-side language such as PHP is for the ability to generate dynamic content. Often this will lead to single scripts that produce their content based on the input parameters (that is, the variables in the URL).

This article covers various techniques and methods for representing these parameters in the URL in a clean and “friendly” manner, as well as then how to read the parameters.

If you’re not quite sure what I mean, take the following example. This website (PhpRiot) stores each of its articles in a database table called articles. Now, we could have built the site so all articles were referenced by their article ID, such as:

  • http://www.phpriot.com/article.php?article_id=1234

However, this isn’t necessarily the best way to do it. Firstly, if people have read a large number of articles on the web site, then their URL history will contain a whole bunch of different IDs, so they won’t be able to directly back to an article without either bookmarking it or visiting the home page and finding the link.

More importantly though, this is wasting valuable data that the search engines can use in indexing your site. PhpRiot has been built in such as way that each articles are accessed in a more human-readable format. For example, a previous article on this site is accessed with the following URL:

  • http://www.phpriot.com/articles/multi-step-wizards

This article will go over how read URLs like this and map them back to the data in your database. There are several methods that can be used with PHP, so we will cover each of these and discuss the pros and cons of each.

Additionally, the ideas used in these may cross-over between each method (such as the manual parsing of URLs), but we will also go over this.

In This Article


Article History

Jan 10, 2006
Initial article version
Feb 28, 2008
Added the "Using mod_rewrite as a 404 Handler" page to the article