HTACCESS adds WWW with HTTPS redirect

Currently my htaccess code is

#add www
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]


#send all traffic to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

      

This works when the following urls are entered

1.https://example.com -> https://www.example.com
2.http: //example.com -> https://www.example.com
3.http: //www.example.com -> https://www.example.com
4.https://example.com -> https://www.example.com
5.https://example.com/somepage -> https://www.example.com/somepage
6.http: //www.example.com/somepage -> https://www.example.com/somepage

But it doesn't work when https and www are missing when trying to access some page, instead it redirects to weird url

7.https: //example.com/somepage -> https://www.example.com/https://example.com/somepage
+3


source to share


1 answer


Replace your current code with this

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

      



Note. You may need to clear your browser cache for it to work onhttp://example.com/somepage

+7


source







All Articles