I found some configuration of nginx contains:
if($args ^~ post=140) {
rewrite ^ http://example.com/ permanent;
}
What does the ^
mean in rewrite ^ http://example.com/ permanent
?
I found some configuration of nginx contains:
if($args ^~ post=140) {
rewrite ^ http://example.com/ permanent;
}
What does the ^
mean in rewrite ^ http://example.com/ permanent
?
It's a regexp metacharacter that matches the beginning of a string. Since all strings have a beginning, this regexp matches any string.
This is a typical nginx idiom for "redirect any URL to http://example.com/
".
The $
metacharacter matching the end of a string would work as well, but ^
seems to be what everybody uses.
^
is the first part of the rewrite
, and what it matches, gets replaced with http://test.com/
. So is this like doing ^.*
, and matching everything? Why does a ^
by itself, work?
foo
will match any URL with the word "foo" in it. The regexp ^foo
will match any URL starting with the word "foo". And so the regexp ^
will match any string at all. .*
would work just as well, but it would probably scan the whole string and thus take longer, I suspect.