When to Use Apache mod_rewrite

Mod_rewrite is for rewriting and redirecting URLs dynamically, using powerful pattern matching to allow for handling of very complex situations. It becomes difficult to give a better definition than that, largely because the uses of mod_rewrite are almost as numerous as the people who use it.

There are, however, a few very common uses, and I aim to cover the majority of these in the examples in this article. The uses of mod_rewrite tend to fall into a few broad categories, as described in the following sections.

"Clean" URLs

Perhaps the most common use of mod_rewrite is to make ugly URLs more attractive. For example, it might be desirable to hide an icky URL like http://www.example.com/cgi-bin/display.cgi?document_name=index and instead have users go to http://www.example.com/doc/index. That can be accomplished very simply with a single RewriteRule, which will allow for an unlimited number of values to appear in place of the "index" in that URL.

The reasons someone might wish to do this vary. Mostly, it's so that the URL is easier to type, easier to remember, easier to tell someone over the phone, easier to put into print - in short, easier.

There are also people who believe that URLs that do not contain question marks, ampersands, and other "special characters" will necessarily appear higher in the rankings on search engines. This is, for the most part, completely untrue. However, a large number of firms billing themselves as "search engine optimization" companies have made large sums of money by persuading people otherwise.

These types of URL rewritings will often be referred to as "clean" URLs, or perhaps as "permalinks" by various software packages. Permalinks, for example, will often remove an ID number in a URL (e.g., http://www.example.com/wordpress/index.php?p=985) and make it more user-friendly (e.g., http://www.example.com/perm/rewritemap). How one URL actually gets translated into the other one is of no concern to the end user, who only really cares that they receive the article they wanted to read.

Mass Virtual Hosting

When you have two or three virtual hosts, manually writing out a <VirtualHost> configuration block for each one is not a big problem. By the time you have a few hundred of them, not only does it become cumbersome to maintain the configuration for all of them, but it also makes Apache take a long time to start up, as it has to load every one of those blocks.

Many people use mod_rewrite to dynamically translate a hostname into a directory path, and are thus able to have an arbitrary number of virtual hosts with a single line in the configuration file. This imposes a number of limitations. In particular, each virtual host has to be identical, in terms of where its document root is located and what options are enabled. But for most ISPs, this is a reasonable limitation, since they have a standard way to set up new customers, and they want those customers to be as similar as possible in order to simplify maintenance.

Site Rearrangement

No matter how carefully you plan your website, you’re going to have to redesign it some day. Part of that redesign is going to involve rearranging your directory structure. What seemed like a good idea a few years ago might turn out to be not so great today. However,you want your old URLs to keep working, because people have them bookmarked.

mod_rewrite will allow you to map your old URL structure to your new URL structure without having to have dozens of redirect statements all over the place. This assumes, of course, that both the former and new directory structures follow a certain logic, so thatmapping one to the other is possible.

And whatever your physical directory structure is, you'll frequently want to have root-level URLs (such as http://www.example.com/press and http://www.college.edu/events), which in fact map to deeper levels in the physical directory structure. You can do this with a Redirect, or you can do it transparently using mod_rewrite. Which of these is "best" depends on a number of factors, many of which just boil down to preference.

Conditional Changes

Many uses of mod_rewrite are conditional. That is, I want the rewrite to happen sometimes,
but not always. These can be based on the time of day, the person who is accessing the website, the user’s preferred language, or any other arbitrary criterion. mod_rewrite allows you to base your rewrite rules on any condition you want to impose or any combination of criteria.

Other Stuff

As soon as you think you've heard every possible use of mod_rewrite, someone will ask for a set of rewrite rules to do something that you've never considered. The amazing thing is that, in most of these cases, there's a way to twist mod_rewrite to do what is desired. It's hard to categorize these weird examples.





Tags: apache,mod_rewrite,virtual hosting

Related Articles