Why does this .htaccess file not contain files that exist?

I have a very simple .htaccess file that is designed to redirect any request to index.php if the file does not exist and is not a directory.

[Before proposed changes]

<IfModule mod_rewrite.c> RewriteEngine On #REWRITE RULES #--------------------- #RULE COMPLETEREWRITE RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^.* index.php </IfModule> 

[Following recommended changes]

 <IfModule mod_rewrite.c> RewriteEngine On #REWRITE RULES #--------------------- #RULE COMPLETEREWRITE RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^.*$ index.php [L] </IfModule> 

(No change between htaccess changes)

Currently, it redirects to index.php regardless of whether the file exists or not. Can anyone explain why this could be?

This seems like a dumb question, but I did a bit of work and changed a bit.

This works in Apache 2.4 under Ubuntu with mod_rewrite enabled, I hope I hope so.

Case scenario:

A file in '/resource/img/panoramas/1.png' exists. It is checked in my VM file system through a local file browser, SSH and FTP.

Prior to the implementation of the htaccess file, this file was deleted remotely. I have a cached version to prove it.

After implementing the htaccess file, any attempt to get along this path returns index.php.

================

The kernel for my CMS contains methods that create headers when using rewrite. If I land on index.php myself, I will not mark the rewrite (as expected). If I hit any other path in this directory, I believe in index.php with a rewrite flag (partially expected). It should not be if the file exists, but still meets contrary to expectations.

It may also be noted that this htaccess file was created automatically from the web.config (IIS) file, and in IIS these rules and my CMS work completely as expected.

Finally (unlike best practices) the entire chmod'ed directory to 777 to exclude the possibility of file inaccessibility.

+7
php apache .htaccess mod-rewrite
source share
5 answers

Try the following:

 RewriteEngine On RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d RewriteRule ^.* index.php 

This is a wild guess. You have to enable logging and publish magazines if we want to provide more than guesses, because the surface of things that can go wrong is big;)

 RewriteLog /var/log/apache2/rewrite.log RewriteLogLevel 5 
+2
source share

You can try this alternative check for files / directories:

 RewriteEngine On RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-l RewriteRule ^ index.php [L] 

Under normal conditions, %{DOCUMENT_ROOT}%{REQUEST_URI} allows the same full path to the file system as %{REQUEST_FILENAME} , but in your case %{REQUEST_FILENAME} will not be allowed.

+2
source share

You can use the 404 Error method to redirect users to index if the file / page does not exist.

Properly:

 ErrorDocument 404 /index.php 
+2
source share

This is very unusual, and this is clearly not the intended result. Therefore, it is safe to assume that the problem may be that another .htaccess file .htaccess interfering with your rewrites. Alternatively, you can have a directive in another Apache configuration file that does this.

To track down the root cause of the problem, you need to tell Apache to write / trace overwrites. When you use Apache 2.4, you need to install LogLevel for your virtual host, for example:

 LogLevel warn mod_rewrite.c:trace4 # where trace<n> is the level of tracing 

(Lateral note: previous versions of Apache used RewriteLog , but now it is deprecated in the latest Apache 2.4.)

You can then enter your site logs, and somewhere you will see the following:

 [Tue Jun 16 16:51:40.123993 2015] [rewrite:trace4] [pid 5412:tid 1680] mod_rewrite.c(475): [client <client_ip_port>] <ip> - - [<your_domain>/sid#1406168][rid#1cb2150/initial] [perdir <document_root>] RewriteCond: input='<document_root>' pattern='!-f' => matched [Tue Jun 16 16:51:40.123993 2015] [rewrite:trace4] [pid 5412:tid 1680] mod_rewrite.c(475): [client <client_ip_port>] <ip> - - [<your_domain>/sid#1406168][rid#1cb2150/initial] [perdir <document_root>] RewriteCond: input='<document_root>' pattern='!-d' => not-matched 

If you do not see these lines (especially the first line), this may mean that the file does not really exist. If the string is present, then I'm afraid that something else is causing the problem. However, using tracing can help you identify the problem so that you can easily solve it.

+1
source share

maybe this is because you caught everything and sent it to index.php in a RewriteRule, try this:

 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] 
0
source share

All Articles