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 found out that the best way for https and www on domain is
Unless you need mod_rewrite for other things, using Apache core IF directive is cleaner & faster:
You can add more conditions to the IF directive, such as ensure a single canonical domain without the www prefix:
There's a lot of familiarity inertia in using mod_rewrite for everything, but see if this works for you.
More info: https://httpd.apache.org/docs/2.4/mod/core.html#if
To see it in action (try without www. or https://, or with .net instead of .com): https://nohodental.com/ (a site I'm working on).
If you're using an Amazon Web Services Elastic Load Balancer which accepts https traffic and routes it to your server(s) with http, the correct way to redirect all http traffic to https is described here: https://aws.amazon.com/premiumsupport/knowledge-center/redirect-http-https-elb
Use the X-Forwarded-Proto header (contains http or https) which is always included in http requests from the load balancer, as described here: https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/x-forwarded-headers.html
In the httpd.conf file:
Or in your root .htaccess file:
Bonus: it will not try to redirect http traffic on your local development machine.
Using the following code in your .htaccess file automatically redirects visitors to the HTTPS version of your site:
If you have an existing .htaccess file:
Do not duplicate RewriteEngine On.
Make sure the lines beginning RewriteCond and RewriteRule immediately follow the already-existing RewriteEngine On.
I like this method of redirecting from http to https. Because I don't need to edit it for each site.
Update: Although this answer has been accepted a few years ago, note that its approach is now recommended against by the Apache documentation. Use a
Redirect
instead. See this answer.Source