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.
The term "mod_rewrite" refers to a module for the Apache web server, which “rewrites” or redirects requests to specified content. Basically, the module transforms incoming requests to a path in the web server's file system. This makes it possible to "rewrite" a URL.
REQUEST_URI is the path component of the URI in the request. For example, /tq_info. php . REQUEST_FILENAME is the result of trying to find the file in the local filesystem by applying the URI path to the document root or any alias that might have been defined.
There are two main directive of this module: RewriteCond & RewriteRule . RewriteRule is used to rewrite the url as the name signifies if all the conditions defined in RewriteCond are matching. One or more RewriteCond can precede a RewriteRule directive.
RewriteRule specifies the directive. pattern is a regular expression that matches the desired string from the URL, which is what the viewer types in the browser. substitution is the path to the actual URL, i.e. the path of the file Apache servers. flags are optional parameters that can modify how the rule works.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With