【问题标题】:.htaccess - removing .html file extension causes 500 internal error (for what should be 404?).htaccess - 删除 .html 文件扩展名会导致 500 内部错误(应该是 404?)
【发布时间】:2019-02-05 20:18:03
【问题描述】:

我正在使用 .htaccess 从我的网址中删除 .html 扩展名。比如……

https://www.example.com/work.html > https://www.example.com/work

如果有人试图访问不存在的文件/目录(例如)https://www.example.com/work/new,我希望能够让网站显示 404 页面(或正确的错误页面)。但这会返回 500 Internal Server Error。

.htaccess 行 ErrorDocument 500 /errors/500.html 不会返回该文件(它适用于 404、403 等)。它返回此错误...

此外,在尝试使用 ErrorDocument 处理请求时遇到 500 Internal Server Error 错误。

Apache 错误日志显示...

AH00124:由于可能的配置错误,请求超出了 10 个内部重定向的限制。如有必要,使用“LimitInternalRecursion”增加限制。使用“LogLevel debug”获取回溯。

我假设这是由于它本质上是试图在 html 文件中查找目录(试图在 html 文件“work.html”中查找目录“new”),因此返回 500 服务器错误而不是 404 .

下面是我的 .htaccess 文件。任何人都可以提供更好的方法或解决方法吗?

# Disable Directory Listing
Options -Indexes

# X-Robots-Tag
Header set X-Robots-Tag "noindex, nofollow"

# Rewrite Engine
RewriteEngine On

# Root Directory
RewriteBase /

# Remove .html Extension
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
RewriteRule (.*)\.html$ $1 [R=301]

# Remove index + Reference Directory
RewriteRule (.*)/index$ $1/ [R=301]

# Remove Trailing Slash **If Not Directory**
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]

# Forward Request To html File, **But Don't Redirect (Bot Friendly)**
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.html [L]

# Errors
ErrorDocument 403 /errors/403.html
ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.html

【问题讨论】:

  • 检查您的 Apache error.log 文件是否出现 500 错误。
  • Apache 错误日志显示...“AH00124:由于可能的配置错误,请求超出了 10 次内部重定向的限制。如有必要,使用“LimitInternalRecursion”增加限制。使用 'LogLevel debug' 获取回溯。”
  • https://www.example.com/work 是否导致 500 错误?

标签: .htaccess url internal-server-error errordocument


【解决方案1】:
RewriteRule (.*)\.html$ $1 [R=301]

所有您的外部重定向需要L (last) 标志,以防止进一步处理。例如:

RewriteRule (.*)\.html$ $1 [R=301,L]

另外,请确保 MultiViews 已禁用:

Options -Indexes -MultiViews

大概,您已经在链接到 URL 没有扩展?

【讨论】:

  • 感谢您的回复。我已经添加了你的建议。所有外部重定向现在都有 L 标志,我还禁用了 MultiViews。仍然收到相同的 AH00124 错误。
  • 另外,是的,我正在链接到没有扩展名的 URL
猜你喜欢
  • 2021-11-07
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多