Forum Moderators: phranque

Message Too Old, No Replies

[mod_rewrite] question mark issue

i think

         

RonPK

2:28 pm on Aug 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm trying to use mod_rewrite in .htaccess to permanently redirect some URLs that somehow got out in the wild and now appear in SERPs:
http://www.mysite.tld/?/widgets

I'd like such requests to be redirected to

http://www.mysite.tld/widgets/

I used PHP to figure out what REQUEST_URI is in this case, and it said

/?/widgets

So I thought that this should work:

RewriteCond %{REQUEST_URI} ^/\?/widgets$ 
RewriteRule ^.*$ ht*p://www.mysite.tld/widgets/ [R=301,L]

But it doesn't work...

I then tried to use QUERY_STRING, which according to PHP contains

/widgets

Unfortunately this rewrite creates an endless loop:

RewriteCond %{QUERY_STRING} ^/widgets$ 
RewriteRule ^.*$ ht*p://www.mysite.tld/widgets/ [R=301,L]

Can someone show me where I'm making a mistake?

jdMorgan

2:45 pm on Aug 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In both cases, you did not clear the existing query string, which caused problems. To clear the query string, append a "?" to your substitution URI. This forces a blank query string. Otherwise, the original query string will be retained.

RewriteCond %{REQUEST_URI} ^/\?/widgets
RewriteRule .* http://www.mysite.tld/widgets[b]/?[/b] [R=301,L]

It's important to keep in mind that a query string is not part of a URI. It is data appended to a URI to be passed to the resource at that URI. Mod_rewrite's query string functions will make a lot more sense if viewed from this perspective.

Jim

RonPK

3:16 pm on Aug 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Jim. I'm now using

RewriteCond %{QUERY_STRING} ^/widgets$ 
RewriteRule .* ht*p://www.mysite.tld/widgets/? [R=301,L]

and that works like a charm.

a query string is not part of a URI

So does this mean that PHP and mod_rewrite have different interpretations of what REQUEST_URI should return? I thought that $_SERVER['REQUEST_URI'] and %{REQUEST_URI} pointed to the same server variable...

jdMorgan

3:30 pm on Aug 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, the problem was that you expected to clear the query string by specifying only a URI in the RewriteRule. Because the query string is handled separately, it is necessary to "manually" clear the query string by appending the "?" to the URI.

Mod_rewrite and PHP do indeed share the same server variable meanings, but it's important to understand that a "?" does not specify a query string in and of itself. Its interpretation is context-dependent. Therefore, "/?xyz" is a query string of "xyz" appended to a request for the index page, but "/?xyz/" is a request for the index page in a directory named "?xyz". Although this latter is a questionably-valid directory name, that is how it is being handled, as evidenced by the fact that you saw "/?/widgets" in the REQUEST_URI variable. It was the fact that there was a trailing slash after the "?" that caused this to be seen as a URI rather than as a query string.

[added] If you want lots more info, see RFC2386 [faqs.org]. Section 5.2 on resolving relative URIs hints at why this happens. Look for the text, "right-most slash". [/added]

Jim