Mode_rewrite trouble

software development


I’m trying to layout my site’s urls where I have a certain group of specific urls AND the rest can be anything. For example:

I want to rewrite admin and list to go to a specific cgi and joe and lisa to go to another one. What I have that doesn’t work:

RewriteRule ^(admin|list)$ /cgi-bin/main.cgi?$1
RewriteRule ^(.+)$ /cgi-bin/special.cgi?$1

From the error logs, I think it’s looping around, matching admin, list, joe and lisa on both rewrite rules.

I’ve looked through a half dozen docs on rewrite but can’t find anything like this. Can anyone point me to a specific example of how to handle this?



Examples? You just need more caffeine… from the mod_rewrite reference documentation:

Additionally you can set special flags for Substitution by appending


as the third argument to the RewriteRule directive. Flags is a comma-separated list of the following flags:

‘last|L’ (last rule)
Stop the rewriting process here and don’t apply any more rewriting rules. This corresponds to the Perl last command or the break command from the C language. Use this flag to prevent the currently rewritten URL from being rewritten further by following rules. For example, use it to rewrite the root-path URL (’/’) to a real one, e.g., ‘/e/www/’.

:cool: -//- One-time [color=#6600CC]$50.00 discount[/color] on [color=#0000CC]DreamHost[/color] plans: Use ATROPOS7


Thanks! You’re right, I always need more caffeine.

I think I figured it out. I was trying to match .+, which doesn’t look like it’s allowed and it makes sense.

RewriteRule ^do$ /cgi-bin/do.cgi
RewriteRule ^(.+)$ /cgi-bin/find.cgi?key=$1

The last flag didn’t help in this situation even it seems like it should. Instead now, I’m matching a little more specifically with the second rule (basically, anything but a - (dash)).


In other words, to keep it from looping you need it to stop once the new URL is that of the CGI script.

Which means you could have used something like:

specific URL

RewriteRule ^(admin|list)$ /cgi-bin/main.cgi?$1[L]

non-specific URL

RewriteCond %{REQUEST_URI} !/cgi-bin/special.cgi
RewriteRule ^(.+)$ /cgi-bin/special.cgi?=$1 [L]

:cool: -//- One-time [color=#6600CC]$50.00 discount[/color] on [color=#0000CC]DreamHost[/color] plans: Use ATROPOS7