htaccess解决.html页面的尾部斜杠

时间:2016-02-08 17:22:11

标签: php .htaccess

如果有人删除了扩展程序并添加了一个尾部斜杠,是否可以将页面解析为它的.html等效内容?

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

http://www.example.com/page/ < - 添加尾部斜杠解析为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]

2 个答案:

答案 0 :(得分:1)

有这样的话:

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]

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

答案 1 :(得分:0)

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

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