Posts tagged: nginx

Redirect www to non-www nginx

Here’s how you redirect www to non-www on nginx. Set this on virtual host and reload nginx.

1
2
3
4
5
6
7
8
9
10
11
server {
  server_name www.example.com;
  rewrite ^ $scheme://example.com$request_uri permanent; 
  # permanent sends a 301 redirect whereas redirect sends a 302 temporary redirect
  # $scheme uses http or https accordingly
}
 
server {
  server_name example.com;
  # the rest of your config goes here
}