I've performed tests in 2-3 apache servers and, if my mind doesn't betray me, %{REQUEST_URI}
always starts with an /
, even if the requested URL is http://domain.com (without and ending slash). However i've seen .htaccess files that have rules like this one:
RewriteRule ^[^/]*$ http...
I mean, would this kind of rule ever match {REQUEST_URI}
? Or maybe i'm missing something about mod_rewrite (isn't the parameter after RewriteRule
just the same as %{REQUEST_URI}
and therefore no match would happen?).
If someone can enlighten me about this, it would be very appreciated.
RewriteRule
patterns do not necessarily match against %{REQUEST_URI}
. From the docs:
What is matched?
In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html").
In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that led the server to the current
RewriteRule
(e.g. "app1/index.html" or "index.html" depending on where the directives are defined).
Since you saw these in an htaccess
file, the path to be matched will not start with a /
, since the prefix removed would contain at least a /
.
.htaccess
is per directory directive and Apache strips the current directory path (thus leading slash if placed in DocumentRoot) from RewriteRule
URI pattern.
%{REQUEST_URI}
on the other hand always contains full URI values starting from /
after domain name.
It will be clear with these examples for this URL:
URL: http://domain.com/path/files/user.php
Case 1: .htaccess
is placed in DocumentRoot
:
%{REQUEST_URI}: /path/files/user.php
RewriteRule pattern: path/files/user.php
Case 2: .htaccess
is placed in DocumentRoot/path/
:
%{REQUEST_URI}: /path/files/user.php
RewriteRule pattern: files/user.php
Case 3: .htaccess
is placed in DocumentRoot/path/files/
:
%{REQUEST_URI}: /path/files/user.php
RewriteRule pattern: user.php
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