Page 1 of 1

regex problem with mod rewrite

Posted: Tue Aug 24, 2004 1:37 am
by John Cartwright
Okay heres my .htaccess

Code: Select all

RewriteRule ^(.*?)/(articles|comments)/(ї0-9]+)$ /?page=$1&mode=$2&id=$3
RewriteRule ^(.*?)/(ї0-9]+)$ /?page=$1&id=$2
which works beautifully for the second rule... which basically means...

http://www.domain.com/pagename/434 ==
include($page) and use $id withen the page

but when i use the first rule
http://www.domain.com/post/comments/43

$page will return as //comments and $mode will not be set and $id will be set to 43.... plz help :)

Posted: Thu Aug 26, 2004 9:01 am
by redmonkey
Without sitting down and trying it, I would guess that the problem is that http://www.domain.com/post/comments/43 actually matches both rules and as a result that type of url is being handled by your last rule.

You can use the 'L' flag on your first rule which should solve the problem. The 'L' flag basically equates to ..... 'If this rule is matched stop here do not check any further rules'. So.....

Code: Select all

RewriteRule ^(.*?)/(articles|comments)/(ї0-9]+)$ /?page=$1&mode=$2&id=$3 їL]
RewriteRule ^(.*?)/(ї0-9]+)$ /?page=$1&id=$2
...should fix the problem. There is no need to use the 'L' flag after the second rule as that is the last rule anyway.

On a side note, I've never been too convinced about the integrity of the regex parser used for mod_rewrite, I have had problems in the past using the '.*?' notation (it has caused 'Internal Server Errors' on older versions) so much so I now actively avoid it. Personally I would be more inclined to write the rules like.....

Code: Select all

RewriteRule ^(ї^/]+)/(articles|comments)/(ї0-9]+)$ ?page=$1&mode=$2&id=$3 їL]
RewriteRule ^(ї^/]+)/(ї0-9]+)$ ?page=$1&id=$2