views:

19

answers:

1

I'm having an issue with my rewrites exposing GET variables if the originating request isn't terminated with a forward slash (/).

Here's my rule:

# All requests are routed to PHP
RewriteRule ^(.*)$ framework/index.php?framework=$1&%{QUERY_STRING} [L]

If I visit:

http://www.domain.com/folder/

everything works great. If I visit:

http://www.domain.com/folder

I get redirected to:

http://www.domain.com/folder/?framework=folder

What gives?

+2  A: 

It sounds like /folder exists in your web root. Since you haven't sent Apache a slash-terminated URL, and the requested resource is a folder, the DirectorySlash directive forces a redirect to the corrected URL.

Unfortunately, mod_rewrite has a go at your request before this redirect occurs, and while it doesn't change the URI that is used in generating the redirect, the changes that it makes to the query string are not separated in a way that mod_dir knows not to include them. Therefore, when the redirect is sent back to the browser, it includes the query string generated by your RewriteRule.

A potential solution to this (other than turning off DirectorySlash, which is not recommended for the reasons listed in the documentation) is to perform mod_dir's work for it as part of your rule set:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{HTTPS}s ^on(s)|off
RewriteRule [^/]$ http%1://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]

RewriteRule ^(.*)$ framework/index.php?framework=$1 [QSA,L]
Tim Stone
Tim, you are legit. Thanks for the awesome response and code. Is there anyway to make the http:// portion of the redirect dynamic? (it may be https)
Kirk
@Kirk - No problem. I think what I've just edited in should take care of the http/https problem, but let me know if you run into issues.
Tim Stone
Wish I could vote you up more than once. Thanks!
Kirk