Apache Mod_Rewrite: Aliases and CGI

Posted on

Apache Mod_Rewrite: Aliases and CGI – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about ubuntu, mod-rewrite, apache-2.2, cgi, .

I am trying to get Apache to run a cgi executable in my “project” directories. It should respond to HTTP requests at:


However, it should not respond to http requests at


To that end, I have created an Apache configuration file which contains:

ScriptAlias /doc_cgi/ /long/path/to/cgi/DocCGI.haskell/

RewriteEngine On
RewriteRule  ^/packages/?$ /doc_cgi/packages/ [L,PT]

<Directory "/long/path/to/cgi/">
     Order deny,allow
     Allow from all
     AddHandler cgi-script .haskell
     Options +ExecCGI

The cgi program is invoked when I make http requests to localhost/doc_cgi/, as expected. But it appears the rewrite rule is not being triggered when I make requests to


for I get 404 errors, and the log says:

[Wed Jul 20 12:29:42 2011] [error] [client] File does not exist: /var/www/packages

For completeness, here are the relevant parts of the configuration file which defines how I handle requests to localhost/packages/PackageName/etc:

AliasMatch ^/packages/(.+)/(.*)  /development/code/haskell/packages/$1/dist/doc/html/$1/$2

<Directory /development/code/haskell/packages/*/dist/doc/html/*/ >
    Options Indexes
    Order deny,allow
    Allow from all

This does work as expected.

Can anybody help me figure out why my rewrite rule isn’t being triggered? Or some hints on how to figure that out myself?

Solution :

Try to escape slashes in the rewrite rule and drop the first slash:

RewriteRule  ^packages/?$ doc_cgi/packages/ [L,PT]

If this doesn’t work you can also enable the Rewrite Log,
Here’s a nice tutorial:


You can post the relevant log entries so we can see

These are the directive from my httpd.conf, just to make sure:

<IfModule mod_rewrite.c>
RewriteLog "/home/user_name/apachelog.log"
RewriteLogLevel 9

Of course replace “/home/user_name/apachelog.log” with your own path.
And of course you’ll have to make HTTP requests so apache will write to the log.

The problem was that if VirtualHosts are enabled, RewriteRules must be used within the context of a VirtualHost or Directory declaration. I am not sure if RewriteRules can be used in a global context if VirtualHosts are not enabled (I think I’ve used them that way before, but maybe there have been changes since Apache 1.3). In any case, Ubuntu’s Apache2 uses VirtualHosts by default. I created a VirtualHost for these components and Included the configuration files I had made.

Leave a Reply

Your email address will not be published.