隐藏 .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]

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...