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条)

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面