Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this .htaccess file not presenting files that exist?

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

[ Before Suggested Modifications ]

<IfModule mod_rewrite.c>
RewriteEngine On

#REWRITE RULES
#---------------------

#RULE COMPLETEREWRITE
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.* index.php

</IfModule>

[ After Suggested Modifications ]

<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 modifications )

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

It seems like a silly question - but I've done a bit of research and have turned up little.

This is running in Apache 2.4 under Ubuntu with mod_rewrite enabled, obviously I hope.

Case Scenario:

The file at '/resource/img/panoramas/1.png' Exists. Verified on my VM filesystem through the local file browser, SSH, and FTP.

Before implementing the htaccess file, this file was accessible remotely. I have a cached version to prove it.

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

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

The core for my CMS contains methods that create headers when a rewrite has been used. If I land on index.php itself, I do not flag a rewrite ( as expected ). If I hit any other path under this directory, I end up at index.php with a rewrite flag ( partially expected ). This should Not be the case if the file exists, but still occurs contrary to expectation.

It may also pay to note that this htaccess file was generated automatically from a web.config (IIS) file, and under IIS, these rules and my CMS both work entirely as expected.

Finally ( contrary to best practices ), the entire directory is chmod'ed to 777, so as to eliminate the possibility of the file being inaccessible.

like image 403
DigitalJedi805 Avatar asked Jun 12 '15 17:06

DigitalJedi805


People also ask

Why can't I see my .htaccess file?

If your . htaccess file is missing, then the first thing you need to do is to visit Settings » Permalinks page and click on 'Save Changes' button without changing anything. WordPress will now try to generate the . htaccess file for you.

Why htaccess is not working in Apache?

In order to verify this, you must open the Apache configuration file (typically either called httpd. conf or apache. conf ) and check that the AllowOverride directive is set to AllowOverride All . If you needed to make changes to your Apache config, remember to save the file and restart Apache.

How do I view .htaccess file?

htaccess file is located in the root directory of your WordPress site. Depending on your hosting provider, the root directory may be a folder labelled public_html, www, htdocs, or httpdocs. You can locate it by using File Manager in your hosting account's cpanel.

Where is .htaccess file located?

htaccess file location is most commonly found in your website's public_html folder. You can access your . htaccess file in a few different ways: From your hosting account's file management (such as via cPanel)


2 Answers

Try this:

RewriteEngine On

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

This is a wild guess. You should enable logging and post the logs if we are to provide more than guesses, because the surface of the things that can go wrong is big ;)

RewriteLog /var/log/apache2/rewrite.log                                                                                                                                                                      
RewriteLogLevel 5
like image 79
rollingBalls Avatar answered Oct 20 '22 00:10

rollingBalls


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]

In normal circumstances %{DOCUMENT_ROOT}%{REQUEST_URI} resolves to same full filesystem path as %{REQUEST_FILENAME} but in your case %{REQUEST_FILENAME} is not getting resolved.

like image 41
anubhava Avatar answered Oct 19 '22 22:10

anubhava