Joy
Joy

Reputation: 287

Nginx - redirect correctly from http:// to https:// without www

Following my configuration file:

server {
   listen [::]:443 ipv6only=off ssl;
   server_name www.example.com;
   // ssl stuff
   return 301 https://example.com$request_uri;
}

server {
   listen  [::]:80 ipv6only=off;
   return 301 https://example.com$request_uri;
}

server {
   listen [::]:443 ssl;  
   server_name example.com;
   // php and ssl stuff
}

I don't understand why http://www.example.com redirects to https://www.example.com and then to https://example.com. How to redirect from http://www.example.com directly to https://example.com?

Upvotes: 1

Views: 128

Answers (2)

Oleh Vasylyev
Oleh Vasylyev

Reputation: 714

NGINX config for redirect from HTTP to HTTPS without WWW:

server {
   listen 80 default_server;
   listen  [::]:80 default_server;
   server_name example.com www.example.com;
   return 301 https://example.com$request_uri;
}
server {
   listen 443 default_server;
   listen [::]:443 ssl http2 default_server;
   server_name example.com www.example.com;
   ##here-ssl-settings##
   return 301 https://example.com$request_uri;
}

Upvotes: 1

Tom
Tom

Reputation: 4826

With HSTS enabled, the first redirect is done directly by your browser without any network interaction.

Upvotes: 0

Related Questions