1

I would like to understand what the proper Nginx configuration is to redirect everything to http://example.com/ ... Namely:

  1. https://example.com/
  2. https://www.example.com/
  3. http://www.example.com/

Need to redirect to http://example.com/ automatically. How do I configure that?

λ Jonas Gorauskas
  • 5,991
  • 6
  • 43
  • 64
  • This question is asked and answered literally every week or so. For https you'll need valid certificate, other than this it's 3-line config – Alexey Ten Jan 23 '15 at 06:51
  • possible duplicate of [Nginx no-www to www and www to no-www](http://stackoverflow.com/questions/7947030/nginx-no-www-to-www-and-www-to-no-www) – knbk Jan 23 '15 at 07:44

1 Answers1

2

You can do something like this:

server {
    listen 80;
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}
server {
    listen   80;
    server_name example.com;
    <your other nginx settings>
}
server {
    listen 443;
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}
server {
    listen 443;
    server_name example.com;
    return 301 http://example.com$request_uri;
}
davko
  • 429
  • 2
  • 13