Forum Moderators: phranque

Message Too Old, No Replies

Rewriting Additional Querrystring Variables

         

stormshield

7:35 pm on Feb 25, 2007 (gmt 0)

10+ Year Member



Is is possible to rewrite querystring? This question implies to a different thing than you probably think. Because what I'm talking about is the querystring that is rewritten to add to additional info for a script. Let me show you what I mean, let's say I'm using this rule:

RewriteRule ^/temp/([^/]+)/([1-9]*).htm template.php?folder1=$1&num=$2 [L]

But sometimes I'd like an extra variable in querystring. For example, I might want to pass the value of $id, typing an url like this:
http://www.example.com/temp/folder/12.htm?id=120

Which I'd like to result in rewriting to this:

http://www.example.com/template.php?subfolder1=folder&num=12&id=120

So, I wrote this rule:

RewriteRule ^/temp/([^/]+)/([1-9]*).htm?([^/]*) template.php?folder=$1&num=$2&$3 [L]

But, well easy to figure, it isn't working for me.

I'd appreciate any suggestion,
Storm

jdMorgan

7:47 pm on Feb 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteRule cannot "see" the query string, so the final part of your pattern will never match anything and $3 will always be empty.

You'll need to use
RewriteCond %{QUERY_STRING} (.+)
to examine the query string appended to the requested URL. You may then back-reference your RewriteCond pattern using %1-%9

(See RewriteCond documentation in Apache mod_rewrite)

Jim

stormshield

8:10 pm on Feb 25, 2007 (gmt 0)

10+ Year Member



Thanks. It works! I used this rewritecond:

rewritecond %{query_string} ^(.*)

plus I added %1 to the rewrite rule.

Too easy? Maybe I'm missing something?:)

jdMorgan

9:14 pm on Feb 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Only that by using that pattern, you're likely adding the ampersand regardless of whether the requested URI has a query string...

Jim