-2

I have SSL issued for domain.com. Now these url works fine:

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

But when accessed with https://www.example.com it shows the Security Certificate error as:

There is a problem with this website’s security certificate.

My nginx config (site-available/example.com) is as follows:

server {
    server_name  www.example.com;
    return       301 https://example.com$request_uri;
}

server {
    listen 80;
    server_name  example.com;
    return       301 https://example.com$request_uri;
}
server {
    listen 443;
    server_name example.com;
    ssl on;
    ssl_certificate /etc/ssl/certs/example_com.crt;
    ssl_certificate_key /etc/ssl/private/example.key;
    ...
    ...
    ...

How can I redirect the url https://www.example.com to https://example.com in this config file

Jenny D
  • 27,780
  • 21
  • 75
  • 114
Jerry Jones
  • 115
  • 1
  • 6

2 Answers2

0

Create a permanent redirect:

server {
      charset utf-8;
      listen 80;
      listen 443 ssl;
      server_name www.example.com;

      rewrite ^/(.*) https://example.com/$1 permanent;
}

server {
      charset utf-8;
      listen 80;
      listen 443 ssl;
      server_name example.com;

      location / {
      # this serves the actual content ...
      }

     if ($ssl_protocol = "") {
       rewrite ^/(.*) https://$server_name/$1 permanent;
     }
}
Tombart
  • 2,143
  • 3
  • 27
  • 48
0

I edited my config as following:

server {
    listen 80;
    server_name  example.com *.example.com;
    return       301 https://example.com$request_uri;
}

server {
    listen 443;
    server_name example.com *.example.com;

<-your_other_site_configurations->
...
...

Now it is working perfectly! Hope it helped others too!!

Jerry Jones
  • 115
  • 1
  • 6