Forum Moderators: phranque

Message Too Old, No Replies

Query String redirect

Is it correct?

         

Lorel

4:53 pm on Oct 11, 2007 (gmt 0)

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



From an earlier discussion on this forum I set up the following query string redirect for when a URL shows up with a "?" on the end of the domain, i.e., www.example.com/?

RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]

However when I check it in an http header checker it shows the following:

#1 Server Response: http://example.com/?
HTTP Status Code: HTTP/1.1 301 Moved Permanently
Date: Thu, 11 Oct 2007 16:46:10 GMT
Server: Apache/1.3.37 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 PHP/4.4.1 FrontPage/5.0.2.2635.SR1.2 mod_ssl/2.8.28 OpenSSL/0.9.7a
Location: http://www.example.com/?
Connection: close
Content-Type: text/html; charset=iso-8859-1
Redirect Target: [aexample.com...]

#2 Server Response: http://www.example.com/?
HTTP Status Code: HTTP/1.1 200 OK
Date: Thu, 11 Oct 2007 16:46:10 GMT
Server: Apache/1.3.37 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 PHP/4.4.1 FrontPage/5.0.2.2635.SR1.2 mod_ssl/2.8.28 OpenSSL/0.9.7a
Connection: close
Content-Type: text/html
X-Pad: avoid browser bug

Is this the correct response, i.e., to not remove the "?"?

jdMorgan

6:00 pm on Oct 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is that the "?" is not part of the query string, nor is it part of the requested URL. Instead, it's only a delimiter between the two. So your code won't catch it.

One way to catch it is this:


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[^?]+\?
RewriteRule (.*) http://www.example.com/$1? [R=301,L]

The RewriteCond pattern reads "match three to nine uppercase characters followed by a space and a slash, followed by any number of characters not a question mark, followed by a question mark, followed by anything."

THE_REQUEST is the entire request header received from the client, for example:

GET /page.html?foo=bar HTTP/1.1

Jim

Lorel

7:28 pm on Oct 11, 2007 (gmt 0)

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



Thanks Jim, I already had that up except for the part about the question mark.

Maybe I had better clarify. I already had this up to make sure index.html is redirected to the root:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.htm\ HTTP/
RewriteRule ^(.*)index\.htm$ http://www.example.com/$1 [R=301,L]

Should I add the rule you just posted below this one or should they both be incorporated into one?

[edited by: Lorel at 7:32 pm (utc) on Oct. 11, 2007]

jdMorgan

7:59 pm on Oct 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No they're totally different!

Use both, with the index\.htm one first.

Jim

jdMorgan

8:06 pm on Oct 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



By the way, this would be faster for your index rule:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.htm\ HTTP/
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.example.com/$1 [R=301,L]

Jim

Lorel

4:38 pm on Oct 12, 2007 (gmt 0)

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



Hi Jim,

thanks for the update on the index to root code. That is working correctly.

However, I'm still getting the same http response for the updated query string as posted above. Is that normal?

jdMorgan

10:26 pm on Oct 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sorry, I 'lost the thread' and coded this to expect a page-path in front of the "?". Allowing for no page-path, you might like this better:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[^?[b]]*\[/b]?
RewriteRule (.*) http://www.example.com/$1? [R=301,L]

Be sure to flush your browser cache completely after updating the code, and before testing.

Jim