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?

davidism
  • 121,510
  • 29
  • 395
  • 339
λ Jonas Gorauskas
  • 6,121
  • 6
  • 45
  • 66
  • 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

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
  • 449
  • 2
  • 13