Matching Multiple Query Strings with Apache Mod_Rewrite

Matching Multiple Query Strings with Apache Mod_Rewrite

John Hewick

For those of you unfamiliar with mod_rewrite, it is basically an Apache webserver module that is designed to help with flexible URL rewriting.  This module is part of the standard Apache installation on all platforms.  As this is such a broad subject, we thought it would be useful to put together a mod_rewrite cheat sheet to make your work a lot easier.  We’ll also provide some examples for practical use, just in case you fancy testing out this module for yourself.

What Does Query String Mean?

If you’re not quite sure what query string refers to, it is the portion of a URL where data passes from a web application to a back-end database.  On the web, any URL can be broken down into three main parts.  Firstly, you have the protocol, then the location of the file/program and finally the query string.  The protocol is almost always HTTP; the location is the standard hostname and filename, and the query string is whatever follows the question mark sign.  For your website to be anything more than a brochure, you will need to store data.  In the URL, you are able to store data using a query string.

Typical URL-manipulation directives such as Alias, Redirect, <Location>, and RewriteRule are unable to directly access the query string.  However, you can use Apache mod_rewrite to add, remove or simply modify the query string of your website.

What is Apache Mod_Rewrite?

The mod-rewrite module basically makes use of a rule-based rewriting engine.  This has the ability to rewrite requested URLs on the go.  By default, the mod-rewrite module will map a URL to a specific filesystem path.  As well as being able to map URLs, you can also use this module to redirect one URL to another URL.  This is particularly handy if you need to set up any redirects from an old URL to a new website URL.

The mod-rewrite module provides a powerful and highly flexible way to manipulate URL’s using an unlimited number of rules.  Simply put, Apache mod_rewrite is used for rewriting your URL at a server level, providing the user output for the final page.

Mod_Rewrite Based on Query String Parameters

The mod_rewrite module is a fantastic tool that makes it super easy for you to create clean URLs, as well as setting up permanent redirects from any legacy URLs to new ones.  However, if you’re looking to rewrite based on an incoming query string, this can be a little bit trickier.  The trick is to make use of RewriteCond to make sure the query string portion of your URL, and a regular RewriteRule match.

For example, say you have recently updated your website and need to setup a redirect from /old-search?query={keywords} to just /search/{keywords}.  The first step in this process will be to set up your ReWrite Rule.  This will automatically redirect any user. As there is no query string for the substitute string, any existing query string will automatically be carried across to your new URL.

Mod_rewriteis a powerful tool, but with this power comes complexity.  It can be particularly difficult to correctly configure this module for your personal use.  Let us know in the comments below if this quick guide helped you understand this complex subject a little more.

John Hewick

Author John Hewick

More posts by John Hewick