简体   繁体   中英

How to redirect from non-secure version of site to secure version of site using .htaccess

I have a multiple website UCC SSL certificate for 4 websites. I want to be sure that a user will get to the secure version of my site no matter how they type in the web address. If the user types in the naked domain name, such as "mysite.com", the .htaccess file that I use will redirect them to https://www.mysite.com . So far, so good. If the user types in "www.mysite.com," the .htaccess file directs them to https://www.www.mysite.com . It adds an extra "www." to the URL. I want to fix it so that the user is redirected to https://www.mysite.com whether they use the www or not.

Here is what I have so far for my .htaccess file:

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

# secure htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>

# Cache files of specific types for 2 weeks
<filesMatch "\.(jpg|jpeg|png|gif|htm|html|css|js)$">
Header set Cache-Control "max-age=1209600, public"
</filesMatch>

The UCC SSL certificate that I have will only secure the site if the www is present (eg. www.mysite.com). It will not secure the naked domain name without the www (eg. mysite.com). Any help would be greatly appreciated. Thank you.

I need to specify something else. The UCC SSL certificate was issued with the main site being for example www.example.com. It also works for just example.com. The other three SAN names are only used with the www in front of them. I hope that makes sense.

This should work:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

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

The only difference is that the www. is removed from the second RewriteRrule .

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