Forum Moderators: phranque
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?
RewriteCond %{REQUEST_URI} ^/\?/widgets
RewriteRule .* http://www.mysite.tld/widgets[b]/?[/b] [R=301,L]
Jim
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...
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