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

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]
    
    已采纳该答案
    打赏 评论
  • drl2051 2011-02-26 15:55

    Have you tried setting the

    RewriteBase /your_directory

    variable?

    打赏 评论

相关推荐 更多相似问题