删除尾随 .html rewriterule 会产生 404 错误

Remove trailing .html rewriterule creates 404 error

我修改了 .htaccess 以从我的页面中删除结尾的 .html。 htaccess 看起来像这样:

RewriteEngine on
RewriteBase /
RewriteCond %{http://www.mydomain.co.uk} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /.html [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.html$ http://www.mydomain.co.uk/ [R=301,L]

我还在 .htaccess 的顶部添加了以下内容以设置自定义 404 页面并停止索引:

ErrorDocument 404 /notfound.html
Options -Indexes

重写规则完美运行。反索引也是如此。但是 404 页面给我这个错误:

Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, webmaster@mydomain.co.uk and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

如果我删除重写规则,那么 404 页面就可以正常工作。我对这类事情知之甚少,想知道如何才能使这两个方面都正常工作?有人可以帮忙吗?

(PS:我读过 this answer 并认为它可能适用于我 - 但根本不理解它。)

只是想让你知道我找到了解决方案:

ErrorDocument 404 /notfound.html

Options -Indexes

RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/\ ]+/)*[^.\ ]+\.htm\ HTTP/
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(([^/]+/)*[^.]+)\.htm$ http://www.example.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://www.example.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(([^/]+/)*[^./]+)$ /.html [L]

效果很好!