Q
Q
q554342017-07-18 15:20:42
Nginx
q55434, 2017-07-18 15:20:42

How to make a 301 redirect in nginx from all domains and subdomains to one?

Good day!
There was a problem, please tell me how to solve it.
There are 2 domains: a.ru and b.ru It is
necessary through 301 redirect all requests from a.ru www.a.ru https://www.a.ru b.ru www.b.ru https://b.ru https://www.b.ru redirect to https://a.ru and if the request comes with a parameter, then take it into account too, i.e. from www.a.ru/123 to https://a.ru/123 , etc.
With a similar nginx configuration:

server {
listen 80;
listen 443;
ssl on;  
ssl_certificate /fullchain.pem;
ssl_certificate_key /privkey.pem;
server_name a.ru www.a.ru b.ru www.b.ru;
return 301 https://a.ru$request_uri;# (rewrite ^/(.*)$ https://a.ru/$1 permanent;  - тоже пробовал, не помогло)
root /www/html;

Firefox "Firefox has detected that the server is redirecting the request for this address in a way that will never complete." and Chrome "redirected you too many times." are not allowed on the site.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Philipp, 2017-07-18
@q55434

If you want your server to work very fast and without loops, you need to create separate groups for each of the domains.

server {
    server_name b.ru;
    return 301 https://a.ru$request_uri;
}

server {
    server_name www.b.ru;
    return 301 https://a.ru$request_uri;
}

server {
    server_name www.a.ru;
    return 301 https://a.ru$request_uri;
}

server {
    server_name a.ru;
    listen 80;
    return 301 https://a.ru$request_uri;
}

server {
    server_name a.ru;
    listen 443 ssl ;
    listen [::]:443 ssl  ipv6only=on;
    ssl on;
    # и так далее
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question