Here's basically what I am attempting to do:

  • When the URL a customer demands indicates personal files or directory, reveal that file
  • Else, redirect to the index.php file

The .htaccess file below is working great the only real problem I am facing is when the URL a customer demands is really a PHP file with $_GET parameters, they're come to the index.php file rather than the file they must be likely to. Any applying for grants the way i can deal with 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 reduces the query string. To append it, the qsappend flag (QSA) ought to be incorporated in most your rewriteRule lines, such as this:

# 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,QSA]

# If requested resource exists as a file, do not rewrite.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?url=$1 [QSA]

// EDIT: You're clearly spinning existing files privately. Just avoid that, but bail out in case your URL hits a current file. (code above edited.)

Problem

# 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]

Shouldn't you be potentially searching for personal files like "somepath.php?querystring" around the filesystem, using this method? I believe that $1 includes the querystring.

So these conditions fail, and also you fall right through to the ultimate rewrite rule. The consumer will get delivered to index.php, however they lose the giveaway querystring because you are not using [cde].


Solution

Try:

[QSA]

Remember # If requested resource exists as a file or directory, go to it RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule (.*) - [QSA,L] to help keep the querystring, if there are any.

You need to combine it with the ultimate rule, too:

[QSA]

Suggestion

Are you certain for you to do no-op rewrites? That's a little odd.

What about killing the logic and mixing individuals two rules? Like so:

# Else rewrite requests for non-existent resources to /index.php
RewriteRule (.*) /index.php?url=$1 [QSA]

Mod_rewrite documentation. Look for # If requested resource doesn't exist as a file or directory, rewrite to /index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule (.*) /index.php?url=$1 [QSA,L]