Here’s essentially what I’m trying to do:
- If the URL a visitor requests points to a file or directory, show that file
- Else, redirect back to the index.php file
The .htaccess file below is working great; the only issue I’m facing is that if the URL a visitor requests is a PHP file with $_GET parameters, they are taken to the index.php file instead of the file they should be going to. Any ideas on how I can fix that?
# Prevent "Index Of" pages
Options -Indexes
# Rewrite
RewriteEngine on
# Rewrite www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# If requested resource exists as a file or directory, go to it
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [L]
# Else rewrite requests for non-existent resources to /index.php
RewriteRule (.*) /index.php?url=$1
Apache normally cuts off the query string. To append it, the qsappend flag (QSA) should be included in all your rewriteRule lines, like this:
// EDIT: You are explicitly rewriting existing files to themselves. Just don’t do that, but bail out if your URL hits an existing file. (code above edited.)