简体   繁体   English

如果不是.htaccess的HTTPS,则重定向到HTTPS

[英]redirect to HTTPS if not HTTPS by .htaccess

I want to redirect user to HTTPS ( https://www.example.com ) if they come with - example.com - www.example.com - http://example.com - http://www.example.com 如果用户随附-example.com-www.example.com- http://example.com-http : //www.example.com,我想将用户重定向到HTTPS( https://www.example.com

I already used this code on .htaccess. 我已经在.htaccess上使用了此代码。 but it's not working for, www.example.com, http://www.example.com 但不适用于www.example.com, http://www.example.com

# BEGIN SSL Redirect
RewriteEngine on
# force ssl
RewriteCond     %{SERVER_PORT} ^80$
RewriteRule     https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R]

RewriteCond %{HTTP_HOST} ^calbel\.com
RewriteRule .* https://www.%{SERVER_NAME}%{REQUEST_URI} [R,L]
# END SSL Redirect

there are dozens of ways, try this one: 有很多方法,请尝试以下一种方法:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

如果不是必须使用.htaccess,为什么不使用:

header('Location: '.$newURL);

Recently I faced the same issue use below code for force redirect to https 最近,我遇到了以下代码中的相同问题,用于强制重定向到https

RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

if not 如果不

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Try it like below, 尝试如下

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www [OR]
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

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

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