Struggling with Apache's ScriptAlias ? Watch out your rewrite rules.

That's a quick tip about a mistake that kept me struggling with an apache configuration for hours. I was trying to enable a cgi-version of php5 on an old, mod-php4 enabled, apache server. As the documentation and my google search results were saying, I ended up with the following configuration statements :


ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

AddHandler x-httpd-php5 .php
Action x-httpd-php5 /cgi-bin/php5

After restarting apache and trying to access a stupid helloworldish /index.php, I got a 404 error and the following statements in the error.log :


[client xx.xx.xx.xx] script '/var/www/-snip-/web/cgi-bin' not found or unable to stat

What I forgot is I have several global RewriteRules to do "mass virtualhosting" on this server :


RewriteRule ^/(.*)$ /var/www/${lowercase:%{SERVER_NAME}}/web/$1

As you can guess, the request /index.php was mapped to /cgi-bin/php5, wich also was remapped, ti /var/www/-snip-/cgi-bin/php5, instead of /usr/lib/cgi-bin/php5.

Fix :


RewriteCond %{REQUEST_URI} !^/cgi-bin/

And it works !