Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does %{REQUEST_URI} always begin with slash?

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.

like image 351
Eduardo Escobar Avatar asked Feb 10 '16 16:02

Eduardo Escobar


2 Answers

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 /.

like image 124
muru Avatar answered Nov 19 '22 11:11

muru


.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
like image 5
anubhava Avatar answered Nov 19 '22 13:11

anubhava