开发者

Apache mod_rewrite Returning Bad Request in 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:2开发者_如何学JAVA4: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?


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]


Have you tried setting the

RewriteBase /your_directory

variable?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜