2

How do you redirect all non-www links to my website to https://www. I know that there are a lot of solutions for .htaccess but I haven't been able to find anything sufficient for nginx.

At the moment all I have is

##
# Redirect non-www to www
##
server {
     listen 80;
     server_name example.com; # add other domains separated by a space as necessary
     rewrite ^/(.*)$ https://www.example.com/$1 permanent;
}

which redirects:

example.com
www.example.com
http://example.com
http://www.example.com

to https://www.example.com which is what I want, except that https://example.com doesn't redirect https://www.example.com

How do so how do I get https:// to redirect to https://www.?

9
  • I would ask why you want to do that. Most people want to get rid of the www. If you recall, many years ago when writing a URL we would include the http(s):// at the beginning. We learned to drop that. Similarly we don't really need the www
    – Steve
    Commented Feb 23, 2017 at 21:58
  • @Steve It's just important for me to know how to do these things. I have been hacking away at this for a bit now and it is very frustrating because I can't get it to work. In addition to that, a lot of people I develop for want this on their websites and it easy to do when you have .htaccess. But this is the first time I'm not using WordPress.
    – Recap
    Commented Feb 23, 2017 at 22:24
  • 1
    @Steve To the contrary, there are legitimate reasons to use the www which is only a subdomain. Some places break their sites into many categories such as the one I was just at 'developers.google.com' and some prefer to keep assets in one area like 'images.mysite.com'.
    – Rob
    Commented Feb 23, 2017 at 22:29
  • I don't see a server listening on port 443 for https but your question is off topic here.
    – Rob
    Commented Feb 23, 2017 at 22:31
  • 1
    @Steve "Most people want to get rid of the www." - The www vs non-www debate is completely irrelevant to this question. (FWIW I'd wager that most people want the www subdomain! :) "might be "legitimate reasons" to use www, it is unnecessary" - if there is a "legitimate reason" then it's not unnecessary.
    – MrWhite
    Commented Feb 24, 2017 at 9:36

2 Answers 2

1

Since my last answer using the "if" statement is not recommended in most cases, I came up with another solution which seems to be working perfectly.

What I did was create another server block with www in the server_name and had my other server blocks redirect to it.

server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;
    return 301 https://www.example.com$request_uri;
}

# HTTPS — proxy all requests to the Node app
server {
    # Enable HTTP/2
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com;
    return 301 https://www.example.com$request_uri;

    # Use the Let’s Encrypt certificates
    ...
}

server {
    # Enable HTTP/2
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.example.com;

    # Use the Let’s Encrypt certificates
    ...
}
0

So far what I came up with is a simple if statement under port 443 to check if the domain is non-www and then redirect to www

server {
# Enable HTTP/2
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
if ($host = example.com) {
    return 301 https://www.example.com$request_uri;
}

# Use the Let’s Encrypt certificates
...

I'm no expert but I don't think this is the best solution because it will be doing an if statement for every request if I'm right?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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