mod_rewrite

The new site takes advantage of Apache’s mod_rewrite. In short, mod_rewrite, allows you to magically “translate” one URL into another without having to redirect the browser.

How could this possibly be useful to programmers? Well it has to do with a little site called Google. Most search engines have complex algorithms judging how valid a URL is. This includees both length of the URL and whether the URL is dynamic or not (URL’s containing GET arguments such as & = and ?). Many web applications use such arguments to dynamically build content, including JAX.

A good example is the default URL for a perm-a-link in my blog:

/jax/index.php/blog/eventHandler=view/entryID=888888888

There are two problems with this URL:

  1. It’s extremely long.
  2. It contains an equal sign, which may keep it from being indexed.

Enter mod_rewrite. The module, through the use of regular expressions, manipulates URL strings. This allows me to turn /jax/index.php/blog/eventHandler=view/entryID=888888888 into /888888888/some-title-text. Below are a few examples.

# Put this in your virtual host definition or a .htaccess file

# Turn the module on
RewriteEngine on

# The first part of the rule rewrites /YYYY/MM into the dynamic
#URL the second part tells mod_rewrite what the "real" URL is
RewriteRule ^/([0-9]{4})/([0-9]{2})$ /view.php?year=$1&month=$2

# The first part of the rule rewrites /888888888/some-title-text
# into the longer dynamic URL
RewriteRule ^/([0-9]{9})/(.*) /view.php?entryID=$1

# This rewrites requests to / (the main index of the page) to
# my real index, which is stored lower in the document tree
RewriteRule ^/$ /jax/index.php/blog

There you have it. Apache’s mod_rewrite makes life a lot easier and should make your site loved much more by many search engines.

1 thought on “mod_rewrite

  1. Yahoo loves them for searches. You need to change the links in your RSS feed too since a lot of SEs are trying to index just feeds 🙂

    Me likey.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.