Forum Moderators: phranque

Message Too Old, No Replies

Query string unexpectedly being passed in redirect

         

dfresh4130

7:02 pm on Feb 17, 2015 (gmt 0)

10+ Year Member



Working on a rule to pull pieces of the query string and pass them onto a different domain in a different format.
http://www.example.com/search/?sku=12345&lot=ABCD --> https://www.test.com/docseaRch/Index.Html#/Search/12345/Abcd
redirect based off the below rewrite block. Any suggestions on what I'm missing?
RewriteCond %{REQUEST_URI} search
RewriteCond %{QUERY_STRING} sku=(.*)&lot=(.*)
RewriteRule ^.* https://www.test.com/docsearch/index.html#/search/%1/%2 [NE,R=301,L]


[Tue Feb 17 14:02:44.931443 2015] [rewrite:trace4] [pid 13051:tid 139869830227712] mod_rewrite.c(475): [client 1.1.1.1:59725] 1.2.3.4 - - [www.example.com/sid#2618900][rid#7f35e0002970/initial] RewriteCond: input='/search/' pattern='search' => matched

[Tue Feb 17 14:02:44.931507 2015] [rewrite:trace4] [pid 13051:tid 139869830227712] mod_rewrite.c(475): [client 1.1.1.1:59725] 1.2.3.4 - - [www.example.com/sid#2618900][rid#7f35e0002970/initial] RewriteCond: input='sku=12345&lot=ABCD' pattern='sku=(.*)&lot=(.*)' => matched

[Tue Feb 17 14:02:44.931526 2015] [rewrite:trace2] [pid 13051:tid 139869830227712] mod_rewrite.c(475): [client 1.1.1.1:59725] 1.2.3.4 - - [www.example.com/sid#2618900][rid#7f35e0002970/initial] rewrite '/search/' -> 'https://www.test.com/docsearch/index.html#/search/12345/ABCD'

[Tue Feb 17 14:02:44.931546 2015] [rewrite:trace2] [pid 13051:tid 139869830227712] mod_rewrite.c(475): [client 1.1.1.1:59725] 1.2.3.4 - - [www.example.com/sid#2618900][rid#7f35e0002970/initial] explicitly forcing redirect with https://www.test.com/docsearch/index.html#/search/12345/ABCD

[Tue Feb 17 14:02:44.931562 2015] [rewrite:trace1] [pid 13051:tid 139869830227712] mod_rewrite.c(475): [client 1.1.1.1:59725] 1.2.3.4 - - [www.example.com/sid#2618900][rid#7f35e0002970/initial] redirect to https://www.test.com/docsearch/index.html#/search/12345/ABCD?sku=12345&lot=ABCD [REDIRECT/301]

[edited by: phranque at 8:10 pm (utc) on Feb 17, 2015]
[edit reason] unlinked URLs [/edit]

dfresh4130

7:17 pm on Feb 17, 2015 (gmt 0)

10+ Year Member



Ok, I found my answer after posting of course. Not sure why it didn't come up in the results sooner. Needed to add the QSD option to the end of the redirect URL like below:
RewriteCond %{REQUEST_URI} search
RewriteCond %{QUERY_STRING} sku=(.*)&lot=(.*)
RewriteRule ^.* [test.com...] [QSD,NE,R=301,L]

I'm used to 2.2 and found using the ? at the end of the destination URL does the same thing as the QSD option in 2.4.

lucy24

9:23 pm on Feb 17, 2015 (gmt 0)

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



I'm used to 2.2 and found using the ? at the end of the destination URL does the same thing as the QSD option in 2.4.

At a savings of three bytes for the older version (comma + 3 characters - one character) ;) But why didn't the rule have the ? there all along? Whether 2.2 or 2.4, it wouldn't have worked as-is.

:: quick detour to Apache docs [httpd.apache.org] ::

Whew. There's no change in default behavior, so rules created in 2.2 will not break.

dfresh4130

10:08 pm on Feb 17, 2015 (gmt 0)

10+ Year Member



I wasn't very familiar with the need to discard query strings on a redirect before digging into creating a rule for myself. I've seen others and just copied them so I never fully understood the need for the ? at the end. I'm not sure I understand why apache chose to make it default behavior to include the query string on a redirect if the destination URI doesn't have one since the QSA option is available.

lucy24

11:25 pm on Feb 17, 2015 (gmt 0)

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



since the QSA option is available

QSA means append the existing query string to the new one. Some types of redirects, such as those created with mod_alias, can't even "see" the query. So it has to pass through unchanged.

The ? at the end of the target really means "replace the old query, if any, with this new stuff". If there's nothing after the ? then the "new stuff" is nothing. Kinda the way my text editor behaves: instead of a separate "delete" command parallel to "replace", find-and-replace includes the option to replace selected text with "" nothing.

QSD is weird, though. It means there are two different ways of doing the identical thing, almost as if there were two different and mutually exclusive defaults.