【发布时间】:2017-01-07 11:49:33
【问题描述】:
我的网站正在更改 URL,并将删除文件扩展名 .html
旧网址:
http://example.com/states.html
新网址:
http://example.com/states
如何设置我的 .htaccess 文件以执行站点范围的重定向,删除 .html 扩展名?
【问题讨论】:
我的网站正在更改 URL,并将删除文件扩展名 .html
旧网址:
http://example.com/states.html
新网址:
http://example.com/states
如何设置我的 .htaccess 文件以执行站点范围的重定向,删除 .html 扩展名?
【问题讨论】:
要从 .html 重定向到无扩展名的 URL,请在 .htaccess 文件顶部附近尝试以下操作(在重写“漂亮” URL 的前端控制器之前)。
RewriteEngine On
RewriteRule (.+)\.html$ /$1 [R=302,L]
当您确定它工作正常时,将302(临时)重定向更改为301(永久)。 301 被浏览器缓存,因此如果出现错误可能会使测试出现问题。
【讨论】:
这应该对你有用:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
【讨论】: