Search code examples
htmlapache.htaccess

Apache: Drop extension but file and folder with same name


Hy guys, I'm finishing to build a static html website under Apache (GoDaddy Linux hosting) but recently I have a problem.

I dropped html extension from my files, however, I have multiple folders with the same name of these files in my directory.

When I try to access any of them, of course, I get the error 403.

Is there any way to solve this through the htaccess file?

My htaccess:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]

I tried several commands suggested in searches by Google, but all were intended to php sites and none worked in my case.

Do you can help me, please? Thanks


Solution

  • I'm going to guess that the problem isn't with your current rules, but that mod_dir is redirecting requests before your rules even get a chance to process the request.

    By default, mod_dir will redirect any request for a directory that's missing the trailing slash to the same request with the trailing slash. That could be what's causing the issue so you need to turn that off.

    As long as you have the Indexes option turned off, it should be ok to turn of directory slashes, otherwise it's possible to expose the directory contents. So try:

    DirectorySlash Off
    Options +FollowSymLinks -MultiViews -Indexes
    RewriteEngine On
    RewriteBase /
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
    RewriteRule ^ %1 [R,L,NC]
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^ %{REQUEST_URI}.html [L]