-1

I want to redirect all users to https://www- version of my website (with htaccess). My problem is that the redirection doesn't work when entering only "www.example.com" in the adress-bar so the browser warns for security. But it works when entering "https://www.example.com".

Here is my code in htaccess:

RewriteEngine On

#first two rows redirects to correct url if non-ssl (if port is not 81)
ReWriteCond %{SERVER_PORT} !^81$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]

#last two rows correct url if non-www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

The ssl certificates are renewed and the hosting company has helped me set the DNS-records.The PHP version is 5.6 (shared hosting).

Is there any other approach more suitable for shared hosting?

What is wrong with my code?

Thankful for your reply!

user1450458
  • 67
  • 1
  • 6

1 Answers1

0

Try this:

RewriteEngine on

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

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Chandra Kumar
  • 4,127
  • 1
  • 17
  • 25
  • Thank you! I then get to the dreaded page with the warning message: The page isn't redirecting properly. So this doesn't work properly :( – user1450458 Aug 07 '17 at 13:40