meteor-up icon indicating copy to clipboard operation
meteor-up copied to clipboard

[Question] Redirect non-www to www

Open damien-monni opened this issue 7 years ago • 8 comments

Is there a way to redirect non-www to www?

I've setup my DNS so both www and non-www are pointing to my app server, but I don't know if I can edit the Nginx config file to redirect non-www to www.

I can't use both as I am using Facebook login, which can only be set up for one domain.

damien-monni avatar Jul 16 '17 14:07 damien-monni

what I did was keep my docker settings mostly as-is (defaults) and then add a new nginx config for a reverse proxy on the server that binds to 127.0.0.1:3000. In the reverse proxy I was able to easily configure nginx

ryanmtaylor avatar Jul 17 '17 20:07 ryanmtaylor

I'm using "force-ssl" package

guncebektas avatar Jul 26 '17 16:07 guncebektas

What is the canonical way to get this done? I'm curious about it and not very well versed with nginx.

Floriferous avatar May 02 '18 21:05 Floriferous

Have you found a way yo solve this. Have same issue

kenshinman avatar Jul 06 '18 07:07 kenshinman

Just to provide more context I tried this nginx.conf:

server {
    listen       80;
    listen       443 ssl;
    server_name  awesome-vulcan.com;
    return       301 https://www.awesome-vulcan.com$request_uri;
}
server {
    listen       80;
    listen       443 ssl;
    server_name  www.awesome-vulcan.com;
}

This fails because the SSL certificate is not associated with my NGINX server (or at least that's how I understand it). So the browser is not happy. If I remove the conf everything works fine, I am just unable to set up the redirection.

Edit: In case you need a fast solution, you can use this solution: https://blog.wax-o.com/2017/11/meteor-galaxy-redirect-non-www-to-www-with-ssl-and-https/ Here is the relevant code (thanks to the original author Fabien Huet):

WebApp.rawConnectHandlers.use(
    (req, res, next) => {

        /**
         * Redirect non-www to www in production
         */
        if (
            process.env.NODE_ENV != 'development' &&
            !req.headers.host.includes('www')
        ) {
            res.writeHead(
                301, {
                    'Location': 'https://www.mydomain.com' + req.originalUrl,
                }
            );
            return res.end();
        }

        /**
         * Keep going
         * /!\ DO NOT DELETE /!\
         */
        return next();

    }
);

Theoritically it could be slower than NGINX based solution though, since you need to process every request to check the URL and it relies on JS.

eric-burel avatar Oct 18 '18 13:10 eric-burel

Also wondering about this. I know this can be done at the DNS level but I'm curious to know how you'd do it with Nginx.

SachaG avatar Nov 26 '18 04:11 SachaG

At the DNS level it is usually not a real redirection, only a CNAME/ALIAS. It may lead to issues with CORS because you are not really redirected. Some DNS providers seem to propose redirection but that's not DNS per se I think. So indeed the best solution is a server such as Nginx that will actually redirect you, I hope we'll manage to figure this out

eric-burel avatar Nov 26 '18 08:11 eric-burel

Is there any solution to this? I am finding myself with the same problem.

juanpmd avatar Mar 28 '21 04:03 juanpmd