mod-rewrite THE_REQUEST and (REQUEST_URI or REQUEST_FILE) give different filenames - why?
I was experimenting with mod-rewrite. I thought what I wanted was simple but I don't get the requested file's URI via REQUEST_URI. Rather the delived name is passed on.
The manual says:
THE_REQUEST
The full HTTP request line sent by the browser to the server (e.g., "GET /index.html HTTP/1.1"). This does not include any additional headers sent by the browser. This value has not been unescaped (decoded), unlike most other variables below.
开发者_如何学编程REQUEST_URI
The resource requested in the HTTP request line. (In the example above, this would be "/index.html".)
However the two give different file names in my tests. I have a bootstrap.php through which I wanted to send all requests. This is the test file:
<?php
echo $_GET['requestedURI'];
?>
on the .htaccess file I have:
### REWRITE RULES ###
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ bootstrap.php?requestedURI=%{REQUEST_URI} [L]
Requesting http://localhost/test.htm
puts out: /bootstrap.php
if I put THE_REQUEST instead of REQUEST_URI in the .htaccess I get GET /test.htm HTTP/1.1
So why not settle for THE_REQUEST? Well, as soon as a query string exists things break. If I request: http://localhost/test.htm?x=1&y=2
I get GET /test.htm?x=1 the first ampersand breaks things. I think it should be possible to replace all ampersands in the querystring with %26 so that it would work but I did not manage so far...
So can anyone tell why REQUEST_URI fails and how to fix it or how to rewrite the ampersands to %26 in the query string?
Thanks.
EDIT: The above report applies to xampp 1.7.3 on Win 7. I tried it on a production Linux system in the meantime and there REQUEST_URI returns what it should.
You don’t need to explicitly pass the requested URI path and query as you can access it in PHP via $_SERVER['REQUEST_URI']
. Thus this should suffice:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ bootstrap.php
I have looked around a bit, and haven't found any good explanation why %{REQUEST_URI}
behaves like it does in your example. The most common way to achieve what your're after seems to be backreferences:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ bootstrap.php?requestedURI=$1 [L]
Edit
Based on your comment, it seems like REQUEST_URI
and REQUEST_FILENAME
are updated and re-evaluated when the rewriterule is triggered.
精彩评论