htAccess 以解析到.html页面的尾部斜杠


htaccess to resolve a trailing slash to .html page

如果有人删除扩展名并添加尾部斜杠,是否可以将页面解析为等效.html页面?

http://www.example.com/page.html

http://www.example.com/page/<-- 向页面添加尾部斜杠解析.html。

但是,我希望尾随斜杠是可选的,而不是我在 htaccess 中添加的东西。

RewriteEngine on
# render .php as .html
RewriteRule ^(.*)'.html$ $1.php [nc]
# don't need extension on the end
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.html [NC,L]
# .php to .html
RewriteCond %{THE_REQUEST} ' /(.+)'.php
RewriteRule ^ /%1.html [L,R=301]

像这样:

RewriteEngine On
# .php to .html
RewriteCond %{THE_REQUEST} ' /(.+)'.php[?'s] [NC]
RewriteRule ^ /%1.html [L,R=301]
# don't need extension on the end
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^(.+)'.html/?$ $1.php [L,NC]

请确保在测试此更改之前清除浏览器缓存。

要使尾部斜杠对 html 请求可选,您可以使用:

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