How do I resolve this .htaccess conflict?

839 views Asked by At

I have the following .htaccess rule in order to enable "clean" URLs, i.e. strip the .html extension from file names:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

It works perfectly in 90 percent of directories on the site, except for directories that have an index.html in them. Linking to those directories as /directory results in the following error:

Forbidden

You don't have permission to access /directory/.html on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Linking to these directories as /directory/index is not desirable, because URLs with "index" in them are not presentable.

To get around this issue, I tried renaming those few index.html files to index.htm, but that results in the same error message when accessing /directory.

I am especially confused about this part:

You don't have permission to access /directory/.html on this server.

Why does Apache think that I am trying to access /directory/.html when in reality I am trying to access index.htm?

Edit: Per Panama Jack's request, I am posting the entire .htaccess file below

Original:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Edit #1:

Options -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ $1.html [NC,L]

Here are a few entries from the apache error log These errors happen with my original .htaccess

client denied by server configuration: /home/user/public_html/ar/.html
File does not exist: /home/user/public_html/403.shtml
client denied by server configuration: /home/user/public_html/zh_TW/.html
File does not exist: /home/user/public_html/403.shtml

These errors happen with the Edit #1 version of htaccess:

File does not exist: /home/user/public_html/en/directory1, referer: http://example.com/en/directory1
Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://example.com/en/directory1
File does not exist: /home/user/public_html/en/directory2, referer: http://example.com/
Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://example.com/
1

There are 1 answers

31
Panama Jack On

Try your rules this way.

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)/?$ $1.html [NC,L]

Edit:

Options -Indexes -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.html -f 
RewriteRule ^(.*)$ $1.html [L,NC]