I'm trying to redirect all insecure HTTP requests on my site (e.g. http://www.example.com
) to HTTPS (https://www.example.com
). I'm using PHP btw. Can I do this in .htaccess?
相关问题
- Angular RxJS mergeMap types
- Backbone.js PushState routes .htaccess only workin
- “Zero out” sensitive String data in Swift
- Stop .htaccess redirect with query string
- Stop .htaccess redirect with query string
相关文章
- C#使用http访问网络,有办法用指定网卡访问网络嘛?
- 请大神帮忙 post向https接口发送数据 部署到服务器为什么运行一会后就会报空指针
- How to get jQuery.ajax response status?
- send redirect and setting cookie, using laravel 5
- Warning : HTML 1300 Navigation occured?
- Is a unicode user agent legal inside an HTTP heade
- git: retry if http request failed
- Security concerns about CORS
I'd recommend with 301 redirect:
Do everything that is explained above for redirection. Just add "HTTP Strict Transport Security" to your header. This will avoid man in the middle attack.
https://en.wikipedia.org/wiki/HTTP_Strict_Transport_Security
If you are in a situation where your cannot access the apache config directly for your site, which many hosted platforms are still restricted in this fashion, then I would actually recommend a two-step approach. The reason why Apache themselves document that you should use their configuration options first and foremost over the mod_rewrite for HTTP to HTTPS.
First, as mentioned above, you would setup your .htaccess mod_rewrite rule(s):
Then, in your PHP file(s) (you need to do this where ever it would be appropriate for your situation, some sites will funnel all requests through a single PHP file, others serve various pages depending on their needs and the request being made):
The above needs to run BEFORE any code that could potentially expose secure data in an unsecured environment. Thus your site uses automatic redirection via HTACCESS and mod_rewrite, while your script(s) ensure no output is provided when not accessed through HTTPS.
I guess most people don't think like this, and thus Apache recommends that you don't use this method where possible. However, it just takes an extra check on the development end to ensure your user's data is secure. Hopefully this helps someone else who might have to look into using non-recommended methods due to restrictions on our hosting services end.
As I was saying in this question, I'd suggest you avoid redirecting all HTTP requests to their HTTPS equivalent blindly, as it may cause you a false impression of security. Instead, you should probably redirect the "root" of your HTTP site to the root of your HTTPS site and link from there, only to HTTPS.
The problem is that if some link or form on the HTTPS site makes the client send a request to the HTTP site, its content will be visible, before the redirection.
For example, if one of your pages served over HTTPS has a form that says
<form action="http://example.com/doSomething">
and sends some data that shouldn't be sent in clear, the browser will first send the full request (including entity, if it's a POST) to the HTTP site first. The redirection will be sent immediately to the browser and, since a large number of users disable or ignore the warnings, it's likely to be ignored.Of course, the mistake of providing the links that should be to the HTTPS site but that end up being for the HTTP site may cause problems as soon as you get something listening on the HTTP port on the same IP address as your HTTPS site. However, I think keeping the two sites as a "mirror" only increases the chances of making mistakes, as you may tend to make the assumption that it will auto-correct itself by redirecting the user to HTTPS, whereas it's often too late. (There were similar discussions in this question.)
A different edge to this problem is when a Load Balancer comes into play.
The situation is as follows: - Traffic from browser to Load Balancer, and back, is (should be) HTTPS - Traffic between Load Balancer and actual WebServer is HTTP.
So, all server request variables in PHP or Apache show that the connection is just HTTP. And the HTTP and HTTPS directories on the Server are the same.
The RewriteCondition in the approved answer does not work. It gives either a loop or it just doesn't work.
Question is: How to get this working on a Load Balancer.
(Or is the Load Balancer configured wrong. Which is what I'm hoping for because then I can move the problem over to the WebHosting company :-) )
This is the proper method of redirecting HTTP to HTTPS using .htaccess according to GoDaddy.com. The first line of code is self-explanatory. The second line of code checks to see if HTTPS is off, and if so it redirects HTTP to HTTPS by running the third line of code, otherwise the third line of code is ignored.
https://www.godaddy.com/help/redirect-http-to-https-automatically-8828