Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

localhost/packages
localhost/packages/

However, it should not respond to http requests at

localhost/packages/PackageName
localhost/packages/PackageName/
localhost/packages/PackageName/more/stuff

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
</Directory>

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

localhost/packages
localhost/packages/

for I get 404 errors, and the log says:

[Wed Jul 20 12:29:42 2011] [error] [client 127.0.0.1] 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
</Directory>

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
add comment

2 Answers

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:

http://perishablepress.com/press/2006/11/28/roll-your-own-apache-rewrite-log/

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
</IfModule>

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
add comment

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
add comment

Your Answer

 
discard

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.