Snippet: htaccess Ignore 'images' and other include folders

Posted on Wednesday 9th November 2011 by Andy Mills

Share this:
  • Share this on Facebook
  • Share this on Twitter
  • Share this on Digg
  • Share this on Del.icio.us
  • Share this on MySpace
  • Share this on tumblr

If you've already had a look at my other tutorial, you'll know that by using the mod_rewrite module on Apache, it's quite easy to turn variable-heavy URL strings into search engine-friendly static URLs.  Furthermore, because these rewrite rules allow for regular expressions to be used for search and replace in the URL string, you can create some pretty generic rules that cover most page requests.

This can, however, lead to some problems further down the line.  Say, for example, you have a rule which turns all URLs in the format of mydomain.com/pageName/subPage.html into mydomain.com/index.php?page=pageName&section=subPage.  You could set this up with a rewrite rule like so:

RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)\.html$ index.php?page=$1&section=$2

... or something along these lines.

This works fine until you start linking to include files, such as images, stylesheets and javascript files.  Let's say you want to place an image somewhere on the website, with the source file being located in the images folder.  The HTML would look like this:

<img src="images/my_image.jpg" alt="My Image" />

With the rewrite rule you set above, however, the source attribute in the image tag would be converted into index.php?page=images&section=my_image.jpg .  So how do you differentiate between include files, such as images, and page requests?  As long as you know which folders your include files are going to be in, you can handle all of the include requests with one rewrite rule:

RewriteEngine on
RewriteRule (images|css|js)/(.*) $1/$2 [L]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)\.html$ index.php?page=$1&section=$2

The part highlighted in bold should be added before any other URL rewrite rules.  Let's break down the rule into each part:

( images | css | js )
The first section of the rewrite rule is simply a list of folders to ignore when performing URL rewriting.  You can add as many folders as you like here, just as long as you separate them with a | character.

( . * )
This just tells us to include any other characters after the reserved folder name to be included in the rewrite path.

$1 / $2
This section basically just rewrites the include path as it was originally written, with $1 being the include folder name, and $2 being the rest of the path.

[L]
This is the most important part of the rewrite rule.  The [L] means 'Last', and tells the server to ignore any rewrite rules after this.

Well, that concludes my little tutorial / snippet.  As you begin to develop more complex web applications on Apache server, you'll find applying generic rules to URL paths extremely useful.