I’ve seen a bunch of ngnix rewrites that have syntax like this:
server {
server_name www.example.com;
rewrite ^(.*) http://example.com$1 permanent;
}
I don’t understand the ^(.*) part. Does the ^ take everything after the TLD of the uri?
The
^does indeed match at the beginning of the string. In the case of nginx’srewritedirective this means the beginning of the path component of the actual URI. Unfortunately nginx’s documentation is slightly incorrect. Quoting from http://www.nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite :However, this is technically wrong.
rewritedoes not match the whole URI/URL but only its path component (which always starts with a/even if the user only enters e.g.http://www.example.cominstead ofhttp://www.example.com/). Thereforerewrite ^(.*) http://example.com$1 permanent;does not turn intohttp://example.comwww.example.com.