Jimmy Adaro
Jimmy Adaro

Reputation: 1405

Apache is trying to access to files as folders

I am running Apache 2.2.22 in Debian with this .htaccess in the site root: file

Options +FollowSymlinks -Indexes -MultiViews
RewriteEngine on
RewriteBase /

#Redirect without www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#Allow access without .php extension
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

That's the main code and we also have a couple of <FilesMatch> instructions and some mod_deflate checks.

The issue is, we're checking this url:

www.example.com/abc

Which is just the abc.php file in the site root, so the server redirects as expected to example.com/abc.

But if I access to (www.)example.com/abc/ the server just throw an HTTP 404 error, which is obvious because the directory /abc/ does not exist.

Shouldn't those defined options avoid that? What may be the issue here?

Upvotes: 1

Views: 23

Answers (1)

anubhava
anubhava

Reputation: 785128

You need to remove a trailing slash using a redirect rule first:

# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [NE,R=301,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)$ $1.php [L]

Upvotes: 1

Related Questions