0

When we open example.com (without HTTPS), the site loaded very well but when we open www.example.com (without HTTPS) it opens "Welcome of nginX" but I do not need this, we want to open example.com even if the URL is www.example.com.

NOTE: our site has "HTTPS" and when we open example.com, it will be redirected to https://example.com.

NOTE: when we open https://www.example.com, the site loaded well but when the URL is www.example.com or https://www.example.com it was not redirected.

We are using Ubuntu at AWS.

Patrick Mevzek
  • 10,995
  • 16
  • 38
  • 54
Leo
  • 867
  • 1
  • 15
  • 41

2 Answers2

1

To do this I would recommend editing your nginx configuration file (usually in /etc/nginx/sites-enabled/you-example-config), adding return 301 as seen here, something like:

server {
    listen 80;
    listen 443;
    server_name www.example.com example.com;
    return 301 https://example.com$request_uri;
    #[...ssl settings go here, certs etc....]
}
server {
    listen 443;
    server_name example.com;
    # [...ssl and main settings go here...]
}

This will cause all requests to return https://example.com

Eric Semwenda
  • 436
  • 6
  • 16
0

Have you defined a separate server for each of the options? See example snippet below. Your question looks very similar to this question and this question which both have a lot more information that I won't copy paste here.

This link might also help creating-nginx-rewrite-rules

server {
    listen 80;
    listen 443 ssl;
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

server {
    listen 80;
    server_name example.com;
    # here goes the rest of your config file
    # example 
    location / {

        rewrite ^/cp/login?$ /cp/login.php last;
        # etc etc...

    }

}

$scheme is the protocol (HTTP or HTTPS) and $request_uri is the full URI including arguments.

Patrick Mevzek
  • 10,995
  • 16
  • 38
  • 54
Kai
  • 1,709
  • 1
  • 23
  • 36