Forum Moderators: phranque

Message Too Old, No Replies

Modrewrite

Trying to get some parameters as dirs, others in query string

         

Thanasus

9:47 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



What I am trying to do is get
[domain.com...]

to change to
[domain.com...]

I've got everything but the search parameter staying in the query string... Here is the rewrite I came up with

RewriteRule ^(.*)/products/(.*)\.html?search=(.*) /search/products.php?product=$2&city=$1&search=$3

jdMorgan

10:05 pm on Mar 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanasus,

RewriteRule cannot "see" the query string directly, so use RewriteCond [httpd.apache.org] to test and extract it, and back-reference it with "%1" :


RewriteCond %{QUERY_STRING} ^search=([^&]*)
RewriteRule ^([^/]*)/products/([^.]*)\.html$ /search/products.php?product=$2&city=$1&search=%1 [L]

Jim

Thanasus

10:19 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



You the man... thanks... makes sense now

Thanasus

12:21 am on Mar 3, 2004 (gmt 0)

10+ Year Member



Quick followup question if I may.. is there a way to make the search parameter if the query string optional?

jdMorgan

12:40 am on Mar 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure there is:

RewriteCond %{QUERY_STRING} ^search=([^&]*)
RewriteRule ^([^/]*)/products/([^.]*)\.html$ /search/products.php?product=$2&city=$1&search=%1 [L]
RewriteRule ^([^/]*)/products/([^.]*)\.html$ /search/products.php?product=$2&city=$1 [L]

Jim

Thanasus

4:47 am on Mar 3, 2004 (gmt 0)

10+ Year Member



Ah...thats it - Thanks. I was trying to figure out some sort of conditional to put in the RewriteCond

Now, if there are other optional parameters in the query string that I would like to keep in the query string...

example: [domain.com...]

The "origin" and "member" parameters may or may not be there. Do I just have 3 of these?

RewriteCond %{QUERY_STRING} ^search=([^&]*)
RewriteCond %{QUERY_STRING} ^origin=([^&]*)
RewriteCond %{QUERY_STRING} ^member=([^&]*)

and then backreference using %1, %2, and %3 like this?

RewriteRule ^([^/]*)/products/([^.]*)\.html$ /search/products.php?product=$2&city=$1&search=%1&origin=%2&member=%3 [L]

The problem I see is what if the URL has only the member parameter or only the origin or only the search parameter? WHat is if has a combo of the two?

Everything I try keeps erroring out, it seems like I literally have to right a RewriteRule for every permutation?

jdMorgan

5:02 am on Mar 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, actually, you don't. But it would be helpful if you could define your entire problem all at once...

Try this instead:


RewriteCond %{QUERY_STRING} (.+)
RewriteRule ^([^/]*)/products/([^.]*)\.html$ /search/products.php?product=$2&city=$1&%1 [L]
RewriteRule ^([^/]*)/products/([^.]*)\.html$ /search/products.php?product=$2&city=$1 [L]

That will copy the entire original query string, including member and search parameters, to the output.

Jim

Thanasus

5:24 am on Mar 3, 2004 (gmt 0)

10+ Year Member



Thats it.. thanks.

I wanted to try to solve it incrementally so I could actually learn all the syntax. I read probably 30 different tutorials on how to use modrewrite but it just wasn't connecting. I couldn't find a good tutorial that really broke down the syntax with examples.