Kristopher Yates did speak thusly:
Hello, Here is an example URL I'm trying to create a RewriteRule for: index.php?section=help&page=test&mode=IN&item=4&action=editI 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 ^(.*)/(.*)/(.*)/(.*)/(.*)$ /index.php?section=$1&page=$2&mode=$3&item=$4&action=$5The above rule correctly passes http://domain.com/help/test/IN/4/edit 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 ^(.*)/(.*)/(.*)/(.*)/(.*)$ /index.php?section=$1&page=$2&mode=$3&item=$4&action=$5 RewriteRule ^(.*)/(.*)/(.*)/(.*)$ /index.php?section=$1&page=$2&mode=$3&item=$4RewriteRule ^(.*)/(.*)/(.*)$ /index.php?section=$1&page=$2&mode=$3 RewriteRule ^(.*)/(.*)$ /index.php?section=$1&page=$2 RewriteRule ^(.*)$ /index.php?section=$1How 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: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.htmlFirst 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: http://domain.com/help http://domain.com/help/ 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. Dragon ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Venimus, Saltavimus, Bibimus (et naribus canium capti sumus) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --------------------------------------------------------------------- The official User-To-User support forum of the Apache HTTP Server Project. See <URL:http://httpd.apache.org/userslist.html> for more info. To unsubscribe, e-mail: users-unsubscribe@xxxxxxxxxxxxxxxx " from the digest: users-digest-unsubscribe@xxxxxxxxxxxxxxxx For additional commands, e-mail: users-help@xxxxxxxxxxxxxxxx
![]() |