Mod rewrite for CGI file to PHP file


Was wondering if anyone can help me out, im trying to redirect a CGI request to a PHP one using a rewrite rule

This is what im trying to use

RewriteRule ^cgi-bin/[A-Za-z0-9-]+) /script/redirect.php?code=$1

So the URL I am redirecting is (can be any code A-Z 0-9) to

The code I am using doesn’t seem to work, am I doing something wrong?

Any help greatly appreciated,


The rewrite rule can only match against the request URI (cgi-bin/, not the query string

You need to use a rewrite condition for that

Try this

RewriteCond %{QUERY_STRING} IT=([A-Za-z0-9-]+)
RewriteRule ^cgi-bin/ /script/redirect.php?code=%1


The cgi-bin is typically a directory OUTSIDE the webspace and it’s an internal directive (which uses mod_alias) to get to. In other words, I’ve failed completely to affect cgi-bin code because of this and can only recommend changing your .pl scripts to redirect for you if that’s the case on your server (assuming that you cannot move the cgi-bin to your WEBSPACE).



Thanks guys! So the only solution is to edit the .pl script to carry out the redirect since it can’t be done in htaccess?


IMHO, IF your cgi-bin is NOT in your website’s directory (in your webspace), then I’m afraid that’s the case.



Thanks for the help David!