简体   繁体   中英

Redirect from https:// to https://www using .htaccess

I am trying to get my site to redirect all traffic to https://www.mylittleblackebook.com no matter how the URL is typed into the browser. There are six ways someone can type the URL into a browser. They are:

  1. https://www.mylittleblackebook.com
  2. https://mylittleblackebook.com
  3. http://www.mylittleblackebook.com
  4. http://mylittleblackebook.com
  5. www.mylittleblackebook.com
  6. mylittleblackebook.com

All of these URL's will work EXCEPT FOR the second one. I cannot get the .htaccess file to redirect https:/mylittleblackebook.com to https://www.mylittleblackebook.com . Here is the code in my .htaccess file:

RewriteEngine on

# require SSL without mod_ssl
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This code works for 5 out of the 6 ways. Can anyone tell me how to get it to work for all of them?

Thank you for taking the time to look this over.

Your code looks correct, not sure why it doesn't work. I would try this instead, it's in the opposite order and a bit different:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) https://www.%{HTTP_HOST}/$1 [L,R,NE] 

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