我使用.htaccess和mod_rewrite来重定向http请求。例如:
RewriteRule ^tutoring/?$ path/to/tutoring.php [L,NC]
我知道Rewrite Engine已启用,因为除了一条规则之外的所有规则都已正确执行。但是有一条规则没有被服务器正确执行,我希望找出原因。规则写为:
RewriteRule ^solutions?/?$ path/to/solutions.php [L,NC]
网址example.com/solution已正确重定向。但是example.com/solutions返回404错误。我尝试修改规则以定位不起作用的特定URL:
RewriteRule ^solutions$ path/to/solutions.php [L,NC]
我得到了相同的结果:404 error
。我还尝试删除除了最后一条规则之外的所有其他.htaccess命令(以及RewriteEngine on)但是,服务器仍然忽略该规则(或者更准确地说只是部分强制执行它,因为/solution
被重定向但是{{1不是)
我不知道为什么会发生这种情况,特别是因为所有其他规则都有效。有问题的规则适用于我的开发机器。在我的研究中,我只遇到过重写不适用于任何规则的情况,而不是为所有规则工作而只是为特定规则工作。
我应该在哪里寻找问题的根源?该站点托管在Apache 2.4 VPS上
//////////////////编辑://///////////////
Apache错误日志显示
/solutions
我尝试通过在.htaccess中添加以下行来启用重写日志,但它们会导致500错误。不知道还能做什么:
AH00687: Negotiation: discovered file(s) matching request: /home/username/public_html/solutions (None could be negotiated).
RewriteLog “/home/username/public_html/rewrite.log
//////////////////决议://///////////////
我发现我的Web根目录中名为solutions.html.BAK的文件导致了问题。试图获取“example.com/solutions”会导致服务器尝试将“重写”规则与“solutions.html.BAK”匹配,而不是“解决方案”。在没有任何规则匹配此请求之后,将发生404错误。删除solutions.html.BAK解决了这个问题。
答案 0 :(得分:1)
您无法从htaccess文件中打开重写日志记录,它需要在您的vhost / server配置中进行。
您获得的错误似乎与mod_negotiation有关,这与多视图有关。尝试关闭它:
Options -Multiviews
如果这不起作用,请尝试在服务器配置中禁用mod_negotiation。
答案 1 :(得分:0)
@Jon Lin,
你的问题非常有用,我已经想出了如何解决我的问题,虽然我仍然不知道是什么问题。
RewriteLog在我的conf文件中不起作用,因为它已从我的Apache(2.4)版本中删除。见http://httpd.apache.org/docs/current/mod/mod_rewrite.html。要打开mod_rewrite日志记录,我使用的命令是LogLevel alert rewrite:trace3
。这会将重写日志条目添加到错误日志中。 (单独的日志会更好,但我不知道如何制作一个)
请记住,我的问题是RewriteRule ^solutions?/?$ bs3/pages/solutions.php
会匹配example.com/solution,而不是example.com/solutions。
:::::::::::::::来自日志::::::::::
请求example.com/solutions
会将以下内容添加到日志中:
strip per-dir prefix: /home/username/public_html/solutions.html.BAK -> solutions.html.BAK
applying pattern '^solutions?/?$' to uri 'solutions.html.BAK'
我不知道为什么服务器会将'solutions'的请求解释为'solutions.html.BAK'。由于这个BAK是我的根目录中的一个文件,我将其删除,问题就消失了。现在,example.com/solutions/
的请求会添加到日志中:
strip per-dir prefix: /home/username/public_html/solutions/ -> solutions/
applying pattern '^solutions?/?$' to uri 'solutions/'
rewrite 'solutions/' -> 'bs3/pages/solutions.php'