Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to force all requests to https? #764

Open
okonon opened this issue Jan 2, 2015 · 12 comments
Open

How to force all requests to https? #764

okonon opened this issue Jan 2, 2015 · 12 comments
Labels
Milestone

Comments

@okonon
Copy link

okonon commented Jan 2, 2015

Hello I would like to mention that this is awesome generator I love it.
I have a quick question.
How would one force all http requests to https? I tried to add some rules to .haccess file but that does not seem to work.
I am running the site on openshift bronze plan.

Thanks a lot,
Happy New Year!

@andrewstuart
Copy link
Member

Assuming you have https and http both set up already and running in the same node app, it's as easy as adding:

app.use(function(req, res, next) {
  if(!req.secure) {
    return res.redirect('https://your-site-name-here');
  }
  next();
});

Of course, you could split this into a redirect module, put it after certain endpoints that don't need to be secure, whatever you want to do.

@kingcody
Copy link
Member

kingcody commented Jan 9, 2015

Great explanation @andrewstuart 👍

@softBarbarian you should be able to use: return res.redirect('https://' + req.headers.host + req.path); to redirect them to the https version of whatever host/path they requested.

@okonon
Copy link
Author

okonon commented Jan 10, 2015

Thanks for your help @kingcody and @andrewstuart Here are my results:
mysite.com -> redirects to https
www.mysite.com -> redirects to https
http://mysite.com -> redirects to https
http://www.mysite.com -> DOES NOT redirect to https
https://mysite.com -> 404
https://www.mysite.com -> no redirect needed, already https

can you please point me to right direction?
Thanks

@kingcody
Copy link
Member

So if

http://mysite.com -> redirects to https

but

https://mysite.com -> 404

where does:

http://mysite.com

redirect to?

@okonon
Copy link
Author

okonon commented Jan 10, 2015

Redirects to https

@kingcody
Copy link
Member

To a 404? I'm only asking because you said that going to https:// manually gives you a 404. So does the redirect to https:// 404 as well?

@okonon
Copy link
Author

okonon commented Jan 10, 2015

Only one of the https requests gives 404 1 the one without www.

So https://www.mysite.com - works
And https://mysite.com - 404

@okonon
Copy link
Author

okonon commented Jan 10, 2015

I wonder if this is registrar/openshift level issue?

@jeffbuhrt
Copy link

softBarbarian,

You might have checked these but check the basics.

confirm DNS points to the same server:
ping www.mysite.com
ping mysite.com
Do they give the same IP? If not then it could be a DNS issue.
(Control-C (^C) to interrupt as needed)

With a browser pointed to https://www.mysite.com do you see your SSL cert?
(Click the lock beside the URL and check 'More Info', etc. depending on the browser.)

You can also do each of:
telnet www.mysite.com 80
telnet mysite.com 80
(once connected type 'GET /', you should see HTTP code in response (your index.html)
If either error it could be the DNS issue above or a server routing if you use a balancer.

try each of:
telnet mysite.com 443
telnet www.mysite.com 443
Do they hang or do you get an error? (Because you won't have SSL negotiated a good IP to a proper server config normally would hang.) Do they error the same way if they error?

Are you on a shared (virtual) host?
For example VHosts in Apache route based on the URL.

What are you using/doing to support SSL?
-Apache, stunnel?
-Something in your code?
Can you see anything in a matching log? (error or SSL request)

@okonon
Copy link
Author

okonon commented Jan 13, 2015

Did not have much time to go thru your steps @jeffbuhrt will do it tomorrow.
However i noticed that my Namecheap's set up as follows:

Host name         IP address/URL               Record type
@                       https://mysite...                Url redirect (301)
www                   app-mysite.rhcloud.com  CNAME (Alias)

This is set up accordingly to OpenShift tutorial.

@kjellski, do you think CNAME entry might be a problem?

@JaKXz JaKXz added the question label Mar 5, 2015
@Awk34 Awk34 added this to the 2.4.0 milestone Jul 16, 2015
@shazimKhan
Copy link

just add this code in your .htaccess file you can easily redirect your page into secure page try its work for me
#Redirect to secure website
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} example.com$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

@punisher21maximum
Copy link

softBarbarian,

You might have checked these but check the basics.

confirm DNS points to the same server:
ping www.mysite.com
ping mysite.com
Do they give the same IP? If not then it could be a DNS issue.
(Control-C (^C) to interrupt as needed)

With a browser pointed to https://www.mysite.com do you see your SSL cert?
(Click the lock beside the URL and check 'More Info', etc. depending on the browser.)

You can also do each of:
telnet www.mysite.com 80
telnet mysite.com 80
(once connected type 'GET /', you should see HTTP code in response (your index.html)
If either error it could be the DNS issue above or a server routing if you use a balancer.

try each of:
telnet mysite.com 443
telnet www.mysite.com 443
Do they hang or do you get an error? (Because you won't have SSL negotiated a good IP to a proper server config normally would hang.) Do they error the same way if they error?

Are you on a shared (virtual) host?
For example VHosts in Apache route based on the URL.

What are you using/doing to support SSL?
-Apache, stunnel?
-Something in your code?
Can you see anything in a matching log? (error or SSL request)

great detailed solutions. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants