Forum Moderators: phranque

Message Too Old, No Replies

Rewrite based on query sting

         

rexell

1:50 pm on Aug 15, 2007 (gmt 0)

10+ Year Member



Hi

I'm trying to make rewrite rule based query string condition
RewriteCond %{query_string} ^brand=12345?lang=en_US$. Based on my understanding this should take those parameters (brand and lang) and make redirect to my new URL, right?

So what I have is:
RewriteEngine On
RewriteCond %{query_string} ^brand=12345?lang=en_US$
RewriteRule ^/foo$ [example.com...] [L]

And I'm posting this request [mydomain.com...] and I should get into
[example.com...] but failing to get there.

Any idea?

[edited by: jdMorgan at 3:04 pm (utc) on Aug. 15, 2007]
[edit reason] example.com [/edit]

jdMorgan

3:44 pm on Aug 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have two question marks in your test URL query string, and an unescaped question mark in your RewriteCond.

That won't work, as your rule will only match:
[example.com...]
-or-
[example.com...]

That is, the unescaped "?" is a regular-expressions token that makes the previous character optional.

For more information, see the regular expressions tutorial cited in our forum charter [webmasterworld.com].

Also, if you are using the word "post" to mean an HTTP POST operation, be aware that in general, HTTP POSTs may not be redirected, due to security restrictions. And in HTTPS, you will additionally run into security certificate problems if you redirect off-site.

Jim

rexell

5:20 am on Aug 16, 2007 (gmt 0)

10+ Year Member



Thanks for the prompt reply.
Based on your comments I would assume that just normal redirect from my 1st URL to 2nd URL sound easier / quicker to implement.

jdMorgan

2:47 pm on Aug 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, you won't be able to use a "normal redirect." If you typed the original URL+query accurately, then you will need to use "extra-extra-special" redirect, because the query string is invalid.

You stated that you tested this URL+query:

[example.com.com...]

The query string violates the HTTP protocol, it that it contains two question marks (highlighted). Because it's invalid, there is no telling what various browsers, search engine robots, or even Apache will do with it.

So if that was typed correctly, then you'll probably need to take extra steps to redirect it. I'd recommend trying something like this:


RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?brand=12345\?lang=en_US$
RewriteRule ^$ https://example.com/foo/?brand=12345&lang=en_US [R=301,L]

This assumes several conditions:

The first RewriteCond assumes that you may have domains in addition to example.com and www.example.com sharing this server "account." If not, you should delete the first RewriteCond.

The second RewriteCond assumes that you only want to redirect this request if it was made via HTTPS. If you wish to redirect it *to* HTTPS for requests via both HTTPS and HTTP, you should delete the second RewriteCond.

The third RewriteCond assumes that you wish this redirect to occur for *all* HTTP methods, that is, GET, POST, HEAD, OPTIONS, etc.
It also assumes (along with the following RewriteRule) that you only wish this redirect to occur if the URL is that of the "home page" or "directory" of the (sub)directory in which the .htaccess file containing this code is located.
It further assumes that the query string will always match exactly, that is, that both name/value pairs will always be present, and in the order shown, always lowercase, and that no other name/value pairs will be present.

The difficult thing about redirecting and rewriting URLs is not writing the code, it is correctly and completely defining the conditions under which the server should (or should not) redirect/rewrite...

If any of the above-listed conditions are wrong or "not quite right," then the code will need to change.

Jim

g1smd

5:11 pm on Aug 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think there was a typo in the original question.

I think one of the "?" was meant to be a "&" instead.