简体   繁体   中英

.htaccess rewrite, trying to remove .html file extension on all files EXCEPT index file

I'm trying to do a "simple" rewrite for all of my files so that you don't have to go to a link with .html at the end of the url. I've got a code that works almost completely, except for the fact that it's rewriting ALL of the files, which then screws up my index.html file and that's kind of a big problem. This is the code that I'm using:

Options FollowSymLinks
DirectorySlash Off
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.html

I guess I just need a code in there that makes it ignore index.html, so I tried this, which gave me error 403 on my index page and error 500s on every other page:

RewriteRule ^index.html$ $0 [L]

But I guess in the end, I'm not entirely sure what I'm doing and I don't know where to put the line of code, and I don't know if it's even entirely correct.

The ORIGINAL code that I tried was this but that gave me error 403s on every page:

Options FollowSymLinks
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.html

What am I doing wrong?

Try this code:

Options FollowSymLinks
RewriteEngine On

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM