rewrite problem with multiple parameters

Discuss practical ways rearrange URLs using mod_rewrite.

rewrite problem with multiple parameters

Postby Guest » Sat Apr 24, 2004 7:17 am

Hi!

Beginner-Question:
I want to rewrite www.myserver.com/XXX/YYY/
to
www.myserver.com/page.php?var1=XXX&var2=YYY

Please, help: How can I make it????
Guest
 

Postby Samuraid » Tue Sep 28, 2004 5:36 pm

RewriteEngine On
RewriteBase /
RewriteRule ^(.*?)/(.*?)/$ /page.php?var1=$1&var2=$2

-----
I think that will do it...
Samuraid
 
Posts: 7
Joined: Tue Sep 28, 2004 5:02 pm

Postby seomike » Tue Sep 28, 2004 8:56 pm

you don't need the ? in the (.*).

. = any one character
*= match previous character 0-infinite times
?= match previous character 1 time.

just a simple (.*) is all it needs :)
seomike
 
Posts: 331
Joined: Thu May 06, 2004 7:36 pm
Location: Dallas

Postby Samuraid » Wed Sep 29, 2004 1:28 pm

but for regular expressions the "?" forces the matching to not be greedy. And it would probably be necessary to keep the RewriteRule from matching:
www.myserver.com/XXX/YYY/ZZZ/

with $1 = "XXX/YYY" and $2 = "ZZZ"

It's just a normal rule of regular expressions...
It might be different for mod_rewrite, but I know with normal regular expressions, you would want the .*? instead of .*
Samuraid
 
Posts: 7
Joined: Tue Sep 28, 2004 5:02 pm

Postby seomike » Wed Sep 29, 2004 10:46 pm

that's why you use conditions and end the rules with a $. I'll admit I'm not a pro at regular expressions but mod rewrite is limited even though it follow the perl regular expression model.
seomike
 
Posts: 331
Joined: Thu May 06, 2004 7:36 pm
Location: Dallas

Postby Samuraid » Fri Oct 01, 2004 12:43 pm

Even if you use the $ on the end, it will still match greedily.

If you match /(.*)/(.*)/$ you are in danger of having RegEx match /(XXX/YYY)/(ZZZ)/$

But if you use .*? it forces the matching to look for the next "/" instead of trying to match as much as possible.

At least this is how it works with perl regular expressions and php preg_ functions as well.


http://www.developer.com/open/article.p ... _3330231_3
Samuraid
 
Posts: 7
Joined: Tue Sep 28, 2004 5:02 pm

Postby seomike » Mon Oct 04, 2004 9:50 pm

You learn something new everyday :)

Thanks for the post and the link!
seomike
 
Posts: 331
Joined: Thu May 06, 2004 7:36 pm
Location: Dallas

Postby Samuraid » Mon Oct 11, 2004 3:04 pm

:)
Samuraid
 
Posts: 7
Joined: Tue Sep 28, 2004 5:02 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 35 guests

cron