V
V
Vladimir Soldatov2016-04-21 16:08:34
Nginx
Vladimir Soldatov, 2016-04-21 16:08:34

How to configure nginx to redirect to without www on all subdomains?

There is an example.com domain that has custom subdomains *.example.com.
How to write a redirect in nginx config so that www.*.example.com addresses become *.example.com?
If from www.example.com to example.com then everything is simple.
server {
server_name www.example.com;
rewrite ^/(.*)$ example.com/$1;
}
And here with subdomains so does not work any more.

server {
    server_name  www.*.example.com;
    rewrite ^/(.*)$ http://*.example.com/$1;
}

In general, the answer was this.
server {
    server_name ~^(www\.)(?<domain>.+)$;
    return 301 $scheme://$domain$request_uri;
}

server {
    listen 80 default_server;
    listen [::]:80;
    server_name ~^(www\.)(?<domain>.+)$;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Soldatov, 2016-04-22
@TPbIHTPABA

server {
    server_name ~^(www\.)(?<domain>.+)$;
    return 301 $scheme://$domain$request_uri;
}

server {
    listen 80 default_server;
    listen [::]:80;
    server_name ~^(www\.)?(?<domain>.+)$;
}

A
Alexander, 2016-04-21
@AlexListen

server {
server_name ~^(?!www\.);
return 301 $scheme://$host$request_uri;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question