+4 votes
in Web & Google by (56.8k points)

I am using the MyVestaCP control panel on my VPS. The webserver is a combination of Apache+Nginx (reverse proxy). I have installed the LetsEncrypt certificate for my domain to access it using only https. But I can access my website using the following URLs:

http:/ /example.com

https:/ /example.com

http:/ /www.example.com

https:/ /www.example.com

 What changes do I need to make in the Nginx config file so that non-https and non-www are redirected to https with www? Basically, I want to give just one URL to the search engines, which will have both https and www.

E.g.

http:/ /example.com -> https:/ /www.example.com

1 Answer

+3 votes
by (74.2k points)
selected by
 
Best answer

When you install VestaCP, MyVestaCP, or HestiaCP, they create two config files for Nginx:

  • domain.nginx.conf
  • domain.nginx.ssl.conf

You need to make changes in both files to redirect all URLs to https with www.

1. domain.nginx.conf

In this file, make the following highlighted changes:

server {

    listen      ip_address:80;

    server_name example.com www.exampl.com;

    return 301 https://$host$request_uri;

2. domain.nginx.ssl.conf

In this file, make the following highlighted changes:

server {

    listen      ip_address:443 ssl http2;

    server_name example.com www.example.com;

    if ($host = 'example.com'){

        return 301 https:/ /www.example.com$request_uri;

        }

The URL is redirected to only https without modifying "domain.nginx.ssl.conf". So, if you want both https and www, you need to modify both files.


...