【问题标题】:htaccess rewrite rules show an error in my error log filehtaccess 重写规则在我的错误日志文件中显示错误
【发布时间】:2016-09-15 19:12:10
【问题描述】:

我有一个依赖于 htaccess 重写规则的网站,我用这样的序列重写我的 URL:

www.example.com/games/car/play/123

实际上没有以下目录:

  1. 游戏
  2. 汽车
  3. 播放

每天我都会看到我的错误日志显示大量错误行:

File does not exist: /home/example.com/public_html/games
File does not exist: /home/example.com/public_html/car
File does not exist: /home/example.com/public_html/play

我不知道为什么 Apache 将这些假目录视为实际的真实目录?我的 htaccess 中有这几行重写代码:

RewriteEngine On
RewriteBase /

RewriteRule ^games/([a-zA-Z0-9-/]+)/play/(.*)$ details.php?slug=$1&id=$2

现在 Apache 考虑:games/slug/play/id 作为目录,它不仅仅是一个虚假的 URL 序列。另外,当我向下浏览页面时:

www.example.com/games/car 和 www.example.com/games

它已经作为 URL 存在,但不是目录我看到错误日志说:

File does not exist: /home/example.com/public_html/games/car
File does not exist: /home/example.com/public_html/games

我该如何解决这个问题?我已经尝试了所有可行的方法,但我的错误日志中仍然出现大量错误。

非常感谢(抱歉我的英语不好)。

【问题讨论】:

  • 在您的 access.log 中,您是否看到对 /games/play 等的请求?
  • "我不知道为什么 Apache 将这些假目录视为真正的真实目录?" - 因为您的RewriteRule pattern 与这些 URL 中的任何一个都不匹配,并且您或其他人正在链接到这些 URL。如果模式不匹配,那么 Apache 就别无选择,只能尝试请求 /games/car 等,这自然会触发 404 Not Found。这些其他 URL 是否应该有效?如果是这样,它们应该映射到什么?
  • @anubhava ,是的,我看到 /games 或 /play 的日志错误太多。这些 URL 是有效的,并映射到页面的类别 slug: category_details.php ,但对于 /play 它不会映射到任何东西。
  • 这意味着你收到了一些无效的http请求,这与重写规则无关。

标签: .htaccess logging url-rewriting rules


【解决方案1】:

尝试关闭 MultiViews 并添加一些条件检查。

Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^games/([a-zA-Z0-9-/]+)/play/(.*)$ details.php?slug=$1&id=$2 [L]

【讨论】:

  • 我会试试这个然后告诉你是否成功!谢谢
  • 非常感谢@Panama Jack,我在错误日志中看不到任何错误,在应用您的精彩代码后所有错误都消失了:) 超级
猜你喜欢
  • 2019-03-27
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-30
  • 2016-09-14
相关资源
最近更新 更多