简体   繁体   中英

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

I already used this code on .htaccess. but it's not working for, 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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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