Apache mod_rewrite

The mod_rewrite module for the Apache web server allows you to change an incoming URL (web page request) or redirect them to another location before the request is processed.

The mod_rewrite configuration can be placed in the httpd.conf (Apache configuration file) or in a .htaccess file within the web tree. If you have a Linux-based, shared-hosting service, you will probably have .htaccess capability. If you run your own server, you can place these directive directly in the httpd.conf file, which is more efficient.

Why use mod_rewrite?

  • If you run a web site, and moved or renamed a file, you can create mod_rewrite rules to change to the new location.
  • When a file is now located on another server.
  • Easy redirects to new locations
  • Well-formed URL‘s to map into your web application. If you have a web application with a “front controller” service into which all requests are sent, this is useful, and perhaps even necessary. With this method, you can create virtual, human-readable URL‘s that walk a logical path to a page, and key information. This makes is easier for bookmarking URL‘s as the application can change without the URL changing. It also looks more professional than using .html, .jsp, .php and other file references (which can change!)

Finding the .htaccess and config files

To use the .htaccess files in the data directory, you must have these configurations in the http.conf file (this is from an apache 1.3.26 release system).

AccessFileName .htaccess
AllowOverride None

.htaccess files can me located in each directory on the web “root” (the base directory pointed to by the web server). Each subdirectory can have additional .htaccess files, and they will be accessed as the server enters each directory looking for the file to serve.

As you can see, this is not as efficient if you have lots of them. It is best to have a single .htaccess file in your web root directory if possible. The alternative is using the httpd.conf file, if you have access.

The Rewrite rules appearing in an http.conf file can be different for each <VirtualHost *> section, or site-wide by appearing as a standard configuration.

Basic Rewriting

So start with, add these lines to the .htaccess (or http.conf file–but I’ll reference .htaccess from here). This code allows me to sent all web requests to the index.php script residing in my web root directory.

RewriteEngine on
RewriteRule ^.*$   /index.php [L]

First, we have to turn the RewriteEngine on. This usually precedes the Rewrite rule configurations.

The RewriteRule directive contains 3 blank-separated parameters

  • URL Regular Expression Match
  • Resulting URL
  • Flags

If the incoming URL is http://cyberdesk.com/section/area/identifier, the string “section/area/identifier” is used for the match.

The Regular Expression matches the incoming URL. The rule is processed only if the match succeeds. If prefixed with a ! symbol, the rule is processed if it does not match. This simple regex reads like this:

  • ^ matches the start of the URL string
  • .* matches any character (.) that occerence 0 or more times (*)
  • $ matches the end of the string

So this will match anything and everything. For more information on Reqular Expressions, see my Regular Expressions page.

When the match succeeds, the string is changed into the new “Resulting URL” string. In this case, all URL‘s are sent to the /web/root/index.php script for processing. In PHP, the requested URL is in the

$_SERVER['REQUEST_URI'] // Contains the leading slash: /section/area/identifier

global variable, so the program can parse that string and determine the resulting page to build.

If the rule doesn’t make the match request, it will move onto the next rewrite rule. If the match is made, the substitution occurs and the rest of the rules are processed with the change. This is done until all RewriteRules are exhausted, or until a Flag tells the Rewrite Engine to do otherwise.

The Flags are directive to tell the Rewrite Engine what to do next. They are places in square brackets and seperated by commas. No spaces should appear in the rewrite rule except separating the parameter fields. Flags are only processed if the match and substitution succeed. The basic flags (and there are more) used are:

  • L (Last) - Treat this like the last rule, no more inspection is done.
  • N (Next) - Restart the Rewrite rules from the top
  • R (Redirect) - Send the HTTP redirect header to change to the new URL. The current request ends.

Samples

To send all but gif graphic requests to the index.php front controller, do:

RewriteEngine on
RewriteRule \.gif$   - [L]
RewriteRule ^.*$   /index.php [L]

This sends all virtual requests (not matching an actual file name or directory) to the front controller.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}       !-f
RewriteCond %{REQUEST_FILENAME}       !-d
RewriteRule ^(.*)$               /index.php?id=$1  [QSA,L]

Conclusion

More tricks for the mod_rewrite can be found at Apache Rewrite Guide

 
programming/web/mod_rewrite.txt · Last modified: 2005/07/20 13:49 by allen