Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RewriteRule - Caret ^ - Match

I am confused about this rule:

RewriteCond %{HTTPS} !=on

RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]

How is it possible that a caret can match the whole URL if it is a position anchor?

I cannot find any official statement that it is a catch all symbol.

like image 854
Masi Avatar asked Dec 31 '16 09:12

Masi


People also ask

What is RewriteCond and RewriteRule?

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.

What is mod_rewrite used for?

mod_rewrite provides a flexible and powerful way to manipulate URLs using an unlimited number of rules. Each rule can have an unlimited number of attached rule conditions, to allow you to rewrite URL based on server variables, environment variables, HTTP headers, or time stamps.

What is Apache RewriteRule?

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.


1 Answers

The caret means looking at the beginning of a line.

The caret matches at the beginning without consuming a character. So even the empty string matches ^. The empty string also matches ^$ since it begins at index 0 and ends at index 0.

The caret matches because the regular expression only needs to be found somewhere in the URL. It doesn't need to match the whole URL.

Using exactly ^ as the regular expression allows for more performance, since typical regular expressions only compare the beginning of the URL and therefore don't need to look at every character of the URL.

like image 58
Roland Illig Avatar answered Oct 13 '22 11:10

Roland Illig