D
D
Dmitry S2016-08-17 19:54:31
Nginx
Dmitry S, 2016-08-17 19:54:31

How to make a 301 redirect to https?

Hello, tell me how to make a 301 redirect
from
site.ru
www.site.ru
https://site.ru
TO
https://www.site.ru

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2016-08-17
@dmitryq1

Redirects must be done on the server. What is your server? Nginx, Apache?
For Apache

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

True, I haven’t worked with him for a long time, maybe someone will correct me. Or, check it out.
UPDATE: Since it turned out that you still have Nginx, here is the config for it:
# http to https
server {
       listen         80;
       listen         [::]:80;
       server_name    example.com www.example.com;
       return         301 https://www.example.com$request_uri;
}
# non-www to www via https
server {
        listen 443 ssl;
        listen [::]:443 ssl;
        ssl_certificate /etc/ssl/certs/*.domain.pem;
        ssl_certificate_key /etc/ssl/private/*.domain.pem;
        server_name example.com;
        return 301 https://www.example.com$request_uri;
}
# Main configuration
server {
        listen 443 ssl http2 default_server; # http2 нужен если вы хотите использовать HTTP/2, иначе не нужно
        listen [::]:443 ssl http2 default_server; # http2 нужен если вы хотите использовать HTTP/2, иначе не нужно
        server_name www.example.com;
        ssl_certificate /etc/ssl/certs/*.domain.pem;
        ssl_certificate_key /etc/ssl/private/*.domain.pem;
        # Остальная конфигурация
        ...
    
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question