question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redirect from http non-www to https www (single redirect)

See original GitHub issue

Hi guys,

I’m trying to redirect http://example.com with a single 301 redirect to https://www.example.com.

While a redirect is easily achieved with some nginx configuration, here’s the problem with the nginx-proxy:

The problem When SSL is enabled (letsencrypt companion) and the non-www alias is added to the VIRTUAL_HOST and LETSENCRYPT_HOST env’s, the nginx proxy automatically creates a server block to redirect from http to https, like this:

server {
	server_name example.com;
	listen 80 ;
	access_log /var/log/nginx/access.log vhost;
	return 301 https://$host$request_uri;
}

So http://example.com will 301 redirect to https://example.com.

While this is neat, my website must redirect to https://www.example.com instead.

Options? I can ofcourse create a redirect in my own proxied webcontainer, that will redirect non-www requests to www, like this:

server {
    listen 80;
    listen 443 ssl;
    server_name example.com;
    rewrite ^(.*) https://www.example.com$1 permanent;
}

The problem is though, this will result in a double 301 redirect:

I want to have a single redirect (for SEO purposes)

The second thing I’ve tried is adding rewrite ^(.*) https://www.example.com$1 permanent; to /etc/nginx/vhost.d/example.com in the nginx-proxy. This will add the configuration to the “listen 443” block only, and this won’t do anything because the http to https from the nginx-proxy will still come first.

Any idea how I can fix this with a single redirect from http:// to https://www…?

Thanks in advance

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:23 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
lukecycacommented, Nov 4, 2019

It seems many of the solutions above “work” but end up using multiple redirects, which is specifically what the OP is trying to avoid.

I finally got this working with a single redirect with the following incantation:

  lukecyca.com:
    image: nginx:alpine
    restart: always
    environment:
        VIRTUAL_HOST: lukecyca.com
        LETSENCRYPT_HOST: lukecyca.com
    volumes:
      - "./var/www/lukecyca.com:/usr/share/nginx/html:ro"

  www.lukecyca.com:
    image: cusspvz/redirect
    restart: always
    environment:
        VIRTUAL_HOST: www.lukecyca.com
        HTTPS_METHOD: noredirect
        LETSENCRYPT_HOST: www.lukecyca.com
        REDIRECT: https://lukecyca.com

The piece that was missing from most other solutions in this (and other) threads was HTTPS_METHOD: noredirect which disables the first redirect by nginx-proxy and allows the container (in this case cusspvz/redirect) to do a single redirect.

lcmbp:~ luke$ curl -v http://www.lukecyca.com/foo
...
< Location: https://lukecyca.com/foo
lcmbp:~ luke$ curl -v https://www.lukecyca.com/foo
...
< location: https://lukecyca.com/foo
lcmbp:~ luke$ curl -v http://lukecyca.com/foo
...
< Location: https://lukecyca.com/foo
lcmbp:~ luke$ curl -v https://lukecyca.com/foo
...
[serves site]
3reactions
adamkdeancommented, Aug 29, 2019

In case it helps anyone, I created https://github.com/adamkdean/redirect to make this easier.

version: '2'

services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    ports:
      - 80:80
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro

  redirect:
    image: adamkdean/redirect
    environment:
      - VIRTUAL_HOST=example.com
      - REDIRECT_LOCATION="http://www.example.com"
      - REDIRECT_STATUS_CODE=301

  example:
    image: example
    environment:
      - VIRTUAL_HOST=www.example.com
Read more comments on GitHub >

github_iconTop Results From Across the Web

htaccess redirect www to non-www with SSL/HTTPS
I've got several domains operating under a single .htaccess file, each having an SSL certificate.
Read more >
How to redirect HTTP to HTTPS and www to non-www
In this – short – article, I show you how you make sure all old links are redirected correctly when you secure your...
Read more >
htaccess redirect to https and www
The first two lines conditionally redirect to https. If the HTTPS variable is set to off, then the request is redirected to https...
Read more >
Apache redirect www to non-www and HTTP to HTTPS
Here I show how to redirect a site from www to non-www (or viceversa) and from HTTP to HTTPS, using the Apache server...
Read more >
How do I redirect www to non-www or vice versa (http and ...
How do I redirect www to non-www or vice versa (http and https)?. To do this, simply create a .htaccess file in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found