keiusui has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am currently using a RewriteRule in htaccess to internally call a Perl script. It works very well for making URLs search engine friendly. For example, when a user goes to:

http://www.mydomain.com/Bill_Gates

They are looking at information about Bill Gates, but they are really calling a script like:

http://www.mydomain.com/cgi-bin/display.pl?name=Bill_Gates

The RewriteRule looks like this:

RewriteRule ^([^/]+)?$ /cgi-bin/display.cgi?name=$1 [L]

This works well, but the htaccess also treats "http://www.mydomain.com/cgi-bin" as a username rediecting it to "http://www.mydomain.com/cgi-bin/display.cgi?name=cgi-bin".

How would I write the RewriteRule so that it calls the Perl script in all cases except when the username is "cgi-bin", "images", or some other common directory, while still treating names like "Bill_Gates" or "Steve_Jobs" as usernames?

Thanks in advance for any help on this.

Replies are listed 'Best First'.
Re: using htaccess to call a perl script
by Anonymous Monk on Feb 26, 2010 at 23:59 UTC
    This is FAQ for apache , so please hit up google / apache forum/docs
Re: using htaccess to call a perl script
by skx (Parson) on Feb 27, 2010 at 11:49 UTC

    You could change your rule to be:

    RewriteRule ^/(.*_.*)/?$  /cgi-bin/display.cgi?name=$1 L
    

    That way it'd only match if the part after the slash had a "_" in it..

    Steve
    --
Re: using htaccess to call a perl script
by Skriptke (Acolyte) on Feb 28, 2010 at 10:55 UTC
    Use RewriteCond
    http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

    If you include a very complicated regex, eventually you will find it difficult to handle, is better:

    RewriteCond -general case or regex-
    RewriteRule -action for general case-

    Then add case:

    RewriteCond -particular case or regex-
    RewriteRule -action for particular case-
    RewriteCond -general case or regex-
    RewriteRule -action for general case-

    No need to remember who did the regex