httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dragon <>
Subject Re: [users@httpd] Mod RewriteRule Help : Accepting Variable Number Of Arguments
Date Sat, 31 Mar 2007 16:56:41 GMT
Kristopher Yates did speak thusly:
>Here is an example URL I'm trying to create a RewriteRule for:
>I have a rule that works when the request URL contains the 
>appropriate number of arguments but does not work when there are 
>fewer arguments than the Rule expects.  I thought by creating 
>multiple (similar) rules would resolve but no luck.  Here is what I 
>have that works as long as I pass all required arguments:
>RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)$ 
>The above rule correctly passes 
> to my script as 
>/index.php?section=help&page=test&mode=IN&item=4&action=edit .
>I tried creating 5 rules so that, if Apache saw fewer arguments, it 
>would go down to the next rule but that didnt work like I thought it would:
>RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)$ 
>RewriteRule ^(.*)/(.*)/(.*)/(.*)$ 
>RewriteRule ^(.*)/(.*)/(.*)$ /index.php?section=$1&page=$2&mode=$3
>RewriteRule ^(.*)/(.*)$ /index.php?section=$1&page=$2
>RewriteRule ^(.*)$ /index.php?section=$1
>How do I write the rule(s) so that, if fewer arguments are passed, 
>it will still work?
>Any help would be greatly appreciated.
---------------- End original message. ---------------------

OK, I will take a shot at this... though I am not very experienced 
with mod_rewrite, I do know fair amount about regular expressions.

mod_rewrite documentation is here:

First off, from the documentation of mod_rewrite, the rules are 
applied in order as defined so they should fall through from one to 
the next if a higher one does not apply. So it seems like your scheme 
should work, why it doesn't is unknown to me. I am not sure about 
this but you might want to try reversing the order of your tests, do 
the smallest number of arguments first.

I think you probably ought to also use the last flag on each rule too 
so it does not skip to the next if it matches. (Now that I think 
about it, this may well be the problem here).

One thing you probably ought to account for is that you are making a 
big assumption in your RE here. That assumption is that there will 
never be a trailing slash on any of the URLs, this may not be a valid 
assumption. To fix that you need to make a change to each RE prior to 
the end of line anchor to indicate either 0 or 1 slashes may exist. 
For example:

         RewriteRule ^(.*)/?$ /index.php?section=$1

This rule would match both of these:

So here is how I suggest you try the rules:

RewriteRule ^(.*)/?$ /index.php?section=$1 [L]
RewriteRule ^(.*)/(.*)/?$ /index.php?section=$1&page=$2 [L]
RewriteRule ^(.*)/(.*)/(.*)/?$ /index.php?section=$1&page=$2&mode=$3 [L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)/?$ 
/index.php?section=$1&page=$2&mode=$3&item=$4 [L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/?$ 
/index.php?section=$1&page=$2&mode=$3&item=$4&action=$5 [L]

Let me know if this helps. I am curious to see if I was on the right track.


  Venimus, Saltavimus, Bibimus (et naribus canium capti sumus)

The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:> for more info.
To unsubscribe, e-mail:
   "   from the digest:
For additional commands, e-mail:

View raw message