6.1. URL Rewriting

If you gathered all the web developers and server administrators in the world, and had them make a list of the most important modules that can be used with Apache, it's a good bet that mod_rewrite will be on that list—and with good reason.

Mod_rewrite, for the unfamiliar, is an Apache module that provides URL rewriting functionality. While that may sound simple, in reality mod_rewrite is the Swiss Army knife of Apache modules. What can you do with mod_rewrite? Outside of the obvious simple URL-rewriting, mod_rewrite gives you the ability to keep query-string input safe, keep your site search-engine-friendly, make your URLs user-friendly, retain "old" URLs after a restructuring, and stop hot linking. You will find that mod_rewrite can quickly become more than just a simple URL-changing robot.

6.1.1. Enabling mod_rewrite

The first thing you'll need to do, before you bother getting your hands dirty setting up mod_rewrite, is to check and see if it's already installed. At the command line, type:

/path/to/httpd -l

Use your own path to Apache's httpd, obviously.

If you see mod_rewrite.c listed, it has already been compiled into Apache. If not, check to see if it exists as a dynamic module for Apache:

ls /path/to/apache/modules/

If mod_rewrite.so exists in the file listing, then Apache can load it as a module with a little tweak to the httpd.conf file, shown later in this section. If it's not compiled into Apache or available as a shared object, you can use whatever ...

Get Professional LAMP: Linux®, Apache, MySQL®, and PHP5 Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.