如何在 htaccess 中排除 .html 并在 url 末尾添加 /

how to exclude .html and add / at the end of the url in htaccess

我有这个 htaccess 代码,它删除了我的 URL 的 .html 扩展名,但我想在 URL 的末尾添加 / 在 .html 扩展被删除了,我该怎么做? 这是我的代码:

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

你可以用这个

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)/?$ .html [NC,L]

然而,此规则不会从 URL 中删除 .html 扩展名,它只是让您可以在浏览器地址栏中键入 exampe.com/file 而不是 example.com/file.html。如果您想从 URL 中删除 html 扩展名,您可以使用以下完整规则:

RewriteEngine On
#redirect /file.html to /file
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ %1/ [L,R=301]
#internally map /file to /file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)/?$ .html [NC,L]