1
votes

I am trying to make simple URL rewriting (example: '/toto.html' will be displayed as '/toto' in the URL bar). I have the following code in my .htaccess file, located at the root of my server (/):

ErrorDocument 403 /
ErrorDocument 404 /
Options All +FollowSymLinks -MultiViews -indexes
# Apache Rewrite Rules (-html extensions)
RewriteEngine On 
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
RewriteRule (.*)\.html$ $1 [L,R=301]

It does work: '/toto.html' is rewrited as '/toto'. However, the server keeps trying to redirect me to the page without the .html extension rather than simply rewriting it. I have read a lot of other documented issues about RewriteEngine being ignored and so on, and tried many things, without any success. The server is still trying to interpret the URL rather than just rewriting it.

1

1 Answers

0
votes

You need one redirect rule and one rewrite rule:

ErrorDocument 403 /
ErrorDocument 404 /

Options All +FollowSymLinks -MultiViews -indexes
# Apache Rewrite Rules (-html extensions)
RewriteEngine On 
RewriteBase /

# To externally redirect /dir/file.html to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html [NC]
RewriteRule ^ /%1 [R=302,L]

# To internally forward /dir/file to /dir/file.html
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]