0

I want to remove www. from both the main site and all sublevel domains. I don't care how many levels into the TLD I am; www is banned from use. It should issue a 301 redirect.

  • www.example.com
  • www.123.example.com

I wrote the following server block, but it doesn't seem to work.

server {
    listen 80;
    server_name ~^(www\.)(?<domain>.+)$;
    return 301 $scheme://$domain/$request_uri;
}
Xeoncross
  • 4,269
  • 12
  • 42
  • 55
  • 2
    Be careful. You will have [trouble using various cloud services](http://serverfault.com/q/408017/126632) if you do not use www. – Michael Hampton Jul 14 '14 at 21:50

2 Answers2

0

Here is how I would do this:

server {
   listen       80;
   server_name  ~^www\.(?<domain>.+?)$;
   return 301 $scheme://$domain/$request_uri;
}
moebius_eye
  • 1,092
  • 7
  • 21
0

From this question:

if ($http_host ~* "^www\.(.+)$"){
rewrite ^(.*)$ http://%1$request_uri redirect;
}
melsayed
  • 1,124
  • 1
  • 6
  • 11