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;
}
0

Here is how I would do this:

server {
   listen       80;
   server_name  ~^www\.(?<domain>.+?)$;
   return 301 $scheme://$domain/$request_uri;
}
  • um, that is what I'm doing minus the capture group. – Xeoncross Jul 14 '14 at 21:40
  • Have you tried it? – moebius_eye Jul 14 '14 at 21:48
  • yes sir, it is the same though. Just one more loose $1 floating around... – Xeoncross Jul 14 '14 at 21:49
  • So, what doesn't work exactly? – moebius_eye Jul 14 '14 at 21:59
0

From this question:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.