Dynamic Parameters

New to mod_rewrite? This is a good place to start.

Dynamic Parameters

Postby lump` » Fri Sep 04, 2009 11:49 pm

Hey guys and dolls,

I'm trying to add dynamic parameters to my rewrite rule.. this is my attempt.

Code: Select all
RewriteEngine On

RewriteRule ^([A-Za-z0-9_]+)/([^/.]+)?$   /TOMS/TestProject/index.php?page=$1&$2   [NC,L]


I want a url such as this -
/TOMS/TestProject/page/?key=val&key2=val2&key3=val3... etc
to point to -
/TOMS/TesProject/index.php?page=page&k=v&k2=v2&k3=v3... etc

It works fine on the 'index.php?page=page' part but putting that dynamic $_GET array in there just doesn't want to pick up ANYTHING.. :(

[rant]
I've spent all morning and plenty of the last few days of my spare time trying to find posts, tutorials or anything about this but not found anything. I'm surprised it's not a common solution! (it probably is and I'm blind) I'm starting to think it's just a dumb idea all together... I have a feeling I've been staring it in the face with a number of 'Rewrite conditions' I've seen but I'm kinda new to the regex and modrewrite so things tend to drift past my recollection at times.. Anyway, anyone who can help would be a star! Or even some advice as to why I'm being a moron, maybe even telling me I'm so smart for trying so hard already, that'd be nice. Hmm..
[/rant]

-Tom
lump`
 
Posts: 4
Joined: Fri Sep 04, 2009 11:30 pm

Postby lump` » Sat Sep 05, 2009 4:17 am

hmmm ok

This works for urls '/TOMS/TestProject/page/key=value&key2=value2' etc but not for '/TOMS/TestProject/page/?key=value&key2=value2' I'm not quite sure how important the question mark is to me...

Code: Select all
RewriteEngine On

RewriteRule ^([A-Za-z0-9_-]+)/([^/.]+)*$   /TOMS/TestProject/index.php?page=$1&$2   [NC,L]


Any advice on improvements?
lump`
 
Posts: 4
Joined: Fri Sep 04, 2009 11:30 pm

Postby richardk » Sat Sep 05, 2009 8:24 am

The QSA flag
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule wrote:'qsappend|QSA' (query string append)
This flag forces the rewrite engine to append a query string part of the substitution string to the existing string, instead of replacing it. Use this when you want to add more data to the query string via a rewrite rule.


Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([a-z0-9_]+)/?$ /TOMS/TestProject/index.php?page=$1 [NC,QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby lump` » Wed Sep 30, 2009 5:43 am

I knew it'd be something as simple as this!! /facepalm :D

thanks a bunch richardk!
lump`
 
Posts: 4
Joined: Fri Sep 04, 2009 11:30 pm


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 38 guests

cron