-2

This question already has an answer here:

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

marked as duplicate by Jenny D, kasperd, Ward, mdpc, Katherine Villyard Feb 10 '16 at 22:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Is the certificate valid for both www.example.com and example.com? – Jenny D Feb 8 '16 at 7:55
  • it is issued for example.com – Jerry Jones Feb 8 '16 at 8:34
  • That is why you get an error message when you are using it for another domain. – Jenny D Feb 8 '16 at 9:47
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;
     }
}
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!!

Not the answer you're looking for? Browse other questions tagged or ask your own question.