【问题标题】:Remove trailing .html rewriterule creates 404 error删除尾随 .html rewriterule 会创建 404 错误
【发布时间】:2015-05-24 19:53:18
【问题描述】:

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

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

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

ErrorDocument 404 /notfound.html
Options -Indexes

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

内部服务器错误 服务器遇到内部错误或配置错误,无法完成您的请求。 请联系服务器管理员 webmaster@mydomain.co.uk 并告知他们错误发生的时间,以及您可能所做的任何可能导致错误的事情。 服务器错误日志中可能提供有关此错误的更多信息。 此外,在尝试使用 ErrorDocument 处理请求时遇到 500 Internal Server Error 错误。

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

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

【问题讨论】:

  • 有人对我有什么想法吗?

标签: apache .htaccess mod-rewrite url-rewriting http-status-code-404


【解决方案1】:

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

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/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.html [L]

效果很好!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    相关资源
    最近更新 更多