Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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.

share|improve this answer
I tried the new RewriteRule and enabling logging. The rule continues to not work, and the rewrite log is empty. – nomen Jul 20 '11 at 22:00
I've added my own httpd.conf directives, also make sure that you remove the slash in both parts of the RewriteRule – fiftyeight Jul 20 '11 at 22:31

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.