‘mod_alias’ and ‘mod_rewrite’

I spent a few hours recently trying to debug some Apache Rewrite rules on our development server.

We had a particular directory outside the root of the website that I was using an Alias command to make available. However, within this directory I also wanted to apply a couple of different Rewrite rules.

A simple cachebuster

One of the rules was basically to remove instances of the string nocache_XXXX_ where XXXX was a number which could change (this is usually a date-stamp, such as 20130131 if modified on 31st Jan, 2013). This allows us to easily change the number in the URL which will force the browser to reload the page, avoiding possible caching problems.

For example:
http://www.example.com/nocache_20130131_js/file.js
will be rewritten as
http://www.example.com/js/file.js
If the file.js changes we can modify the number in the url, which will force the browser to load the JavaScript again as it is a different URL, but the web server will return the current “file.js” inside the “js” directory, after the rewrite rule has been applied.

To cut a long story short, I found out that mod_alias and mod_rewrite cannot be used together. Therefore, the workaround in my case was to use just mod_rewrite and use a symbolic link instead of trying to use mod_alias. Note that to allow symlinks to be use, it is also necessary to specify Options FollowSymLinks within the Apache config for that site.

Maybe this will help someone else avoid wasting time trying to get mod_rewrite to work on top of mod_alias.

2 thoughts on “‘mod_alias’ and ‘mod_rewrite’

  1. Neat trick! Just to save somebody else a bit of google time in case they also land here:

    This page explains hos mod_alias and mod_rewrite get processed when used together, as well as showing a couple of other useful things:

    http://my.opera.com/GreyWyvern/blog/2007/09/12/apache-mod-rewrite

    And by the way I find debugging mod_alias to be a royal pain, perhaps even worth porting Alias directives to mod_rewrite just to get access to the rewrite_log directive. There probably is a way to get good debugging in mod_alias but I have yet to find it.

    • Hi Erik

      Thanks for the comment and that link, it looks like a good explanation of how mod_alias and mod_rewrite work together. I’ll have to have a proper read through it sometime soon as I’m sure it’ll help the pain I’m having with rewriting.

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.