Apache mod_rewrite and RewriteRule's are incredibly important as websites get increasingly competitive for placement in the SERP. In most instances, if you're using a framework, there is a strong probability that this will be included in the underlying platform and it'll just be a case of invoking it. This post simply gives you an introduction to the concept and some resources for further reading.

Essentially it boils down to three things:

  1. Write some server-side script to transform your "cruft(ed)" URL's into something more meaningful. i.e. Transform catalog.php?catID=2_4 into something like catalog/kitchen/kitchen-cabinets/ One way you could do this is have an SEO "slug" for each category in your database. In this example cat id: 2 has slug: "kitchen" and cat id: 4 has slug: "kitchen-cabinets". One thing I will stress is it's important to give some serious consideration to your "cruft-free" URL pattern structure. For example: In MVC architecture it's generally considered as controller/action/var1/var2/var3 etc... The reason for designing a solid URL pattern is it will be incredibly useful for (and simplifying) your RewriteRule's!
  2. Now! Hands-up, I'll admit I'm not the greatest when it comes to Regular Expressions and pattern matching. For me it's a combination of (limited) knowledge, Google and trial & error! The next step is to add some RewriteRule's to your Apache config', usually in an .htaccess file, but in rare instances, they can be also be placed in a .conf file.

    RewriteEngine on
    RewriteBase /

    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

    RewriteCond %{REQUEST_URI} !\.(?:css|png|jpe?g|gif)$ [NC]
    RewriteRule ^catalog/(.*)/$ catalog.php?catID=$1

    This is where it gets a little involved and you'll spend a few hours wading through Apache Docs and Google trying to perfect your .htaccess RewriteRules!

    1. First thing is turn RewriteEngine on. There is an assumption that mod_rewrite is available, for most, it is, but you could wrap your directives in an <IfModule mod_rewrite.c> block to be on the safe side.
    2. The RewriteBase directive, although not always necessary, is used to provide a relative path base for rules, it works somewhat similar to the <base> tag in HTML.
    3. The RewriteCond directive, again not always required, specifies conditions for subsequent rule processing
    4. Lastly, the RewriteRule itself. This is supplied in the format: RewriteRule [pattern] [target] [flags], the [pattern] component of this may take some tuning, as, if like me you're not to great with RegEx. You'll find tons of pointers via. Google, but this page in the Apache Doc's goes over mod_rewrite in a lot more detail, especially their Regular Expressions

      Both RewriteRule and RewriteCond also support the use of "flags" to modify their behavior. For example [NC] = nocase. Some of the other more common flags are [QSA] = qsappend (Query String Append), [L] = last, and [R] = redirect with a valid HTTP status code. For a complete list of flags, go here.

  3. Lastly. A point of note. Without RewriteCond, your rules will be applied to ALL URL's in a document. Including references to CSS/JS/Images etc... which may have a very undesired result if those have been referenced with relative paths in your HTML markup. Two simple fixes. Make all those URL's absolute, or, if like me you don't like this idea, simply add a <base> HTML tag to your pages.