duanao6704 2011-02-26 15:49
浏览 72
已采纳

Apache mod_rewrite在WAMPServer中返回错误请求

I'm running Windows 7 64 bit with WAMPServer 2.1. The version of Apache running is 2.2.17. I'm trying to set up a simple rewrite rule in httpd.conf:

<IfModule rewrite_module>
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [PT,QSA,L]
</IfModule>

I've gotten simpler RewriteRules to work previously, without regex, rewrite expressions, and the like. It seems that at this point, however, I'm getting a 400 Bad Request error whenever attempting to visit any URL when this rule is active. The Apache log file gives these errors whenever this happens:

[Sat Feb 26 10:24:18 2011] [error] [client 127.0.0.1] Invalid URI in request GET /index.php HTTP/1.1
[Sat Feb 26 10:24:21 2011] [error] [client 127.0.0.1] Invalid URI in request GET / HTTP/1.1

The first error I tried hitting localhost/index.php. The second I just tried localhost.

Furthermore, even when I had the simpler rule working, the RewriteCond lines didn't seem to be working: static existing files such as style.css were being rerouted anyway.

Enabling RewriteLogging just seems to crash Apache, which appears to be a documented issue on 64-bit machines running Windows 7. Any ideas as to what the problem may be?

  • 写回答

2条回答 默认 最新

  • douyu2817 2011-02-27 03:31
    关注

    There's two issues here that are likely contributing to your troubles. First, you can't check for file existence so easily in a server context (as you are with httpd.conf), because the request hasn't been mapped to the file system yet.

    If your requests for static resources happen to correspond directly to your file system structure beyond your DOCUMENT_ROOT, you can solve this by appending the value of DOCUMENT_ROOT before the incoming REQUEST_URI, as follows:

    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
    

    If the path to your static resources is transformed somewhere down the line, the mod_rewrite documentation suggests using the following, which will issue a subrequest to perform a lookahead based on the request URI:

    RewriteCond %{LA-U:REQUEST_FILENAME} !-f
    RewriteCond %{LA-U:REQUEST_FILENAME} !-d
    

    This approach is more expensive though, so try to use the first one where at all possible.

    Secondly, the URI that you pass back into the request is actually invalid, because it needs the leading slash. Correcting that and combining it with the new conditions above gives you this modified rule set:

    RewriteEngine On
    
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
    RewriteRule ^.*$ /index.php?page=$0 [PT,QSA,L]
    

    Note that the value of page here will always have a leading slash, so you might actually want to use this rule instead, depending on your handling logic in index.php:

    RewriteRule ^/(.*)$ /index.php?page=$1 [PT,QSA,L]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?