如何解决隐藏 .html 并管理 404
我想在我的网站上做这个映射
xyz.html -> on /xyz
abc.html -> on /abc
index.html -> on /
我在我的 .htaccess 文件中写了这些规则
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !^$ !.*\.html$ %{REQUEST_FILENAME}.html [L]
但我有一些问题。
- 404 错误不再起作用。如果我浏览 /hello 它给了我内部服务器错误
- 如果我浏览到 /xyz.html -> /xyz(SEO 友好),我想要重定向
想要的动作:
/ -> serve index.html
/abc -> serve abc.html
/abc.html -> redirect to /abc
/123.html -> throws 404
/123/ -> throws 404 (now throws 500)
我该怎么做?提前致谢
解决方法
您可以使用以下内容:
RewriteEngine on
#Remove .html extension
#1)redirect "/file.html" to "/file"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^([^.]+)\.html$ /$1 [L,R=301]
#2) internally map /file to /file.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^/]+)/?$ /$1.html [L]
#3) Redirect "index.html to "/"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.html$ / [L,R=301]
#Internally map homepage "/" to "index.html"
RewriteRule ^$ /index.html [L]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。