regex problem with mod rewrite

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

regex problem with mod rewrite

Post 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 :)
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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
Post Reply