D
D
Dymok2018-06-15 16:20:26
Nginx
Dymok, 2018-06-15 16:20:26

How to set up a redirect from http to https and from www to no-www?

In general, the essence of the question is in the title. I tried googling and came up with this

example.com
#redirect

server {
    listen              80;
    server_name "~^(www\.)?(.*)$" ;
    return 301 https://$2$request_uri ;
}


#В этом блоке пытался оставлять строки с ssl и закомментированными и раскоментированными 
server {
        listen 443 ssl;
        server_name "~^www\.(.*)$" ;
        return 301 https://$1$request_uri;
        #ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
        #ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
        #include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        #ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
        #listen [::]:443 ssl ipv6only=on; # managed by Certbot
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

        #root directory
        root /var/www/example.com/public;
        #domain name
        server_name example.com;
        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm;

}


Redirects work with this config:
http://example.com -> https://example.com
http://www.example.com -> https://example.com

But when redirecting from https://www.example.comthe browser swears at the certificate. CHADNT? Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
isrofilov, 2018-06-15
@UnluckySerivelha

Your certificate must contain two domains with and without www. Or create a separate server with a certificate from www

R
RidgeA, 2018-06-15
@RidgeA

and doesn't it work?
server_name example.org www.example.org;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question