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,709

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,113
0

From this question:

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