RegEx Match Help

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

RegEx Match Help

Post by aliasxneo »

I'm using some rewrite rules that look like this:

Code: Select all

RewriteEngine on
RewriteRule ^([A-Za-z].*)/ index.php?query=$1 [nc]
RewriteRule ^([A-Za-z].*)/\?(.*) index.php?query=$1&get=$2 [nc]
Here's what should happen:

Code: Select all

http://www.mysite.com/test/test1/  produces:
http://www.mysite.com/index.php?query=test/test1
 
http://www.mysite.com/test/test1/?somevar=value  produces:
http://www.mysite.com/index.php?query=t ... =somevalue
The first url rewriting works fine. It's the second one that won't work. I've went and simulated what the url would look like and tested the regex I have for the second rule against it and it worked perfectly (returned test/test1 and somevar=somevalue). I've no idea why it won't work here though. Does anyone have any alternative RegEx or can someone explain what I might be doing wrong? Thanks.
whiterabbit
Forum Newbie
Posts: 14
Joined: Thu May 22, 2008 6:58 pm

Re: RegEx Match Help

Post by whiterabbit »

I think if you switch the order and add a [L], that should do it. Otherwise, I can help you with a new reg-ex.

Code: Select all

 
RewriteEngine on
RewriteRule ^([A-Za-z].*)/\?(.*) index.php?query=$1&get=$2 [nc, L]
RewriteRule ^([A-Za-z].*)$/ index.php?query=$1 [nc]
 
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Re: RegEx Match Help

Post by aliasxneo »

whiterabbit wrote:I think if you switch the order and add a [L], that should do it. Otherwise, I can help you with a new reg-ex.

Code: Select all

 
RewriteEngine on
RewriteRule ^([A-Za-z].*)/\?(.*) index.php?query=$1&get=$2 [nc, L]
RewriteRule ^([A-Za-z].*)$/ index.php?query=$1 [nc]
 
What's the L do? I switched the order and nothing happened. Then I added the L and got an internal server error.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: RegEx Match Help

Post by GeertDD »

aliasxneo wrote:What's the L do? I switched the order and nothing happened. Then I added the L and got an internal server error.
The L flag stands for "last rule".
See: http://rewrite.drbacchus.com/rewritewiki/Flags_2fL

You are getting an internal server error because of the space inside [NC, L]. Change it to [NC,L].
Post Reply