I am using mod_rewrite to send queries to PHP for handling in a CMS. My problem: if the query is the name of a directory, the query that gets sent to PHP is added to the URL.
Here is the code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /process.php\?query=$1 [QSA,NC,L]
So if the user types in
http://example.com/lolcats
mod_rewrite silently redirects to
http://example.com/process.php?query=lolcats
which is great. But if lolcats is a directory, mod_rewrite redirects (NOT silently) to
http://example.com/lolcats/?query=lolcats
adding the query to the end of the original request. Apache still serves the PHP output, but it changes the URL in the user’s address bar.
So I need to stop the query from being added to the request even if the query is the name of a directory.
This is a
DirectorySlashissue, where apache redirects with a trailing slash when you try to access a directory.You can either turn
DirectorySlash Off(noting that there is a security warning concerning turning this off), or try to have mod_rewrite handle it preemptively with something like:THough if you are routing all existing directories through
process.php, the security warning can probably be ignored.