简体   繁体   中英

How to redirect https://www to https:// and http://www to https:// and http:// to https://

I need to redirect all the following addresses :

  • http://www.example.com
  • https://www.example.com
  • http://example.com

to https://example.com using a permanent redirect in an .htaccess file.

I'd try the following:

RewriteEngine On

# URL with www rewrite to https without www
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]

# URL without www rewrite to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)(.*) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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