O'Reilly logo

PHP Hacks by Jack D. Herrington

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #60. Make Usable URLs with mod_rewrite

Use Apache's mod_rewrite module to create URLs that are easy to understand and use.

The Apache server's mod_rewrite module gives you the ability to redirect one URL to another transparently, all without the user's knowledge. This opens up all sorts of possibilities, from simply redirecting old URLs to new addresses, to cleaning up the "dirty" URLs (filled with extra parameters and data your application will never use) coming from a poor publishing system—turning them into URLs that are friendlier to both readers and search engines.

An Introduction to Rewriting

Readable URLs are nice. A well-designed web site will have a logical filesystem layout with smart folder names and filenames and as many implementation details left out as possible. In the better-designed sites, readers can even guess at filenames with a high level of success.

However, sometimes the best possible design still can't stop your site's URLs from being nigh impossible to use. For instance, you might be using a content management system that serves out URLs that look something like http://www.site.com/viewcatalog.php?category=hats&prodID=53.

This is a horrible URL, but it and its brethren are becoming increasingly prevalent in these days of dynamically generated pages. There are a number of problems with a URL of this kind:

  • It exposes the underlying technology of the web site (in this case, PHP). This can give potential hackers clues as to what type of data they should send, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required