简体   繁体   English

如何从http重定向到https

[英]How to make redirect from http to https

How to redirect from http to https: 如何从http重定向到https:

1. redirect http://example.com to https://www.example.com
2. redirect http://www.example.com to https://www.example.com
3. redirect https://example.com to https://www.example.com

Like facebook, google? 像Facebook,谷歌?

Please help me with code in .htaccess or another. 请使用.htaccess或其他代码帮助我。

To handle all these 3 redirects in a single rule you can use this code in your DOCUMENT_ROOT/.htaccess file: 要在单个规则中处理所有这3个重定向,您可以在DOCUMENT_ROOT/.htaccess文件中使用以下代码:

RewriteEngine On

RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"' [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,R=301,L]

RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

just add below to your .htaccess 只需将以下内容添加到您的.htaccess中

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteEngine On
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM