主页的 Htaccess 404 问题

Htaccess 404 issue with homepage

我的htaccess 404规则设置如下:

ErrorDocument 404 /404.htm

但事情是这样的:

不存在 url,没有扩展名: https://www.example.com/bgbgbgbgbg redirects to https://www.example.com/404.htm正确的行为

不存在 url 带点: https://www.example.com/bgbgbgbgbg. redirects to https://www.example.com/404.htm正确的行为

不存在 url 扩展名: https://www.example.com/bgbgbgbgbg.htm redirects to "file not found" (should redirect to https://www.example.com/404.htm)

url 以斜杠结尾: https://www.example.com/bgbgbgbgbg/ redirects to a weird 404 page with no css, and the urls in the browser's address bar does not change (should redirect to https://www.example.com/404.htm)

我做了一些研究并尝试在 ErrorDocument 404 /404.htm:

下实现这些行
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . https://www.example.com/404.htm [L]

它解决了所有问题,但它重定向了主页 (https://www.example.com) to https://www.example.com/404.htm

如何解决主页问题?

要将不存在的请求重定向到 404 错误页面,您可以简单地使用:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.+$ /404.htm [L,R]