2

i have been searching for redirecting my site from www.domain.com to domain.com

i followed this Nginx no-www to www and www to no-www

but for normal http://www.domain.com it is redirecting to domain.com

but when i try https://www.domain.com it stays as it is, not redirecting to https://domain.com

i don know what i'm missing

server {
    listen 80;
    server_name www.domain.com;
    // $scheme will get the http protocol
    // and 301 is best practice for tablet, phone, desktop and seo
    return 301 $scheme://domain.com$request_uri;
}

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

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

    }
}
Community
  • 1
  • 1
Anbazhagan p
  • 943
  • 1
  • 14
  • 27
  • Please consider posting your nginx config. – bredikhin Feb 21 '14 at 15:21
  • i followed exactky like in the first answer of http://stackoverflow.com/questions/7947030/nginx-no-www-to-www-and-www-to-no-www, i have edited my question which one i follwed from the link – Anbazhagan p Feb 21 '14 at 15:58
  • So, then you are probably listening to https on the wrong port, the answer below is pretty straightforward. – bredikhin Feb 21 '14 at 18:02

1 Answers1

4

You need to set up another server to listen to the https and redirect it

server {
  listen 443 ssl;
  server_name www.example.com;
  # add ssl config to avoid certificate warning
  return 301 https://example.com;
}

You can sum both http and https redirection in one server

server {
  listen 80 443;
  server_name www.example.com;
  ssl on;
  # ssl config
  return 301 $scheme://example.com;
}
Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89