9

I've installed a ssl cert on a site and it's working. Now I want to redirect visitors coming in on http over to https

I've put this code and only this code in the htaccess file, but it's causing a redirect loop.

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.mysite.com/$1 [R,L]

</IfModule>

My original htaccess looked like this:

<IfModule mod_rewrite.c>
RewriteEngine On

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

# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>

Could Craft somehow be causing the redirect loop?

In the Craft CP I have the Site URL set to https://www.mysite.com

2
15

For reference, this worked:

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
2
  • 1
    This still causes an ERR_TOO_MANY_REDIRECTS for me – Duncan Luk Feb 22 '17 at 10:18
  • This worked for a while for me, but now it causes ERR_TOO_MANY_REDIRECTS – Keith M Nov 18 '17 at 19:49
14

I recommend trying out the Patrol plugin. It avoids .htaccess headbanging and just handles the redirects via PHP.

Patrol Plugin for Craft CMS

In the Settings, turn on Secure Connections and add a backslash / to the Protected Areas settings box. That will force SSL across the board. If you want to get more specific, you can provide multiple, more-specific paths.

1
  • 1
    The settings changed. Now you need to put 'patrol' => ['forceSsl' => true, 'restrictedAreas' => ['/']] in craft/config/general.php – Duncan Luk Feb 22 '17 at 10:36
5

Try this sample htaccess-file I'm using on another site.

RewriteEngine On
RewriteBase /

# If the request is not secure
RewriteCond %{HTTPS} off
# Redirect to the secure version
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

# Normal redirect rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L] 
2
  • Thanks. Even with just that code in the htaccess, I'm still getting a redirect loop. – user1070143 Oct 14 '14 at 13:25
  • I edited my answer with a complete code. Can you try that one? – naboovalley Oct 14 '14 at 13:30
1

Because I have so little reputation I cannot comment or like anything.

But @naboovalley 's answer did the trick for me! all the other ones gave me

ERR_TOO_MANY_REDIRECTS!

So the right answer to this question is probably:

RewriteEngine On
RewriteBase /

# If the request is not secure
RewriteCond %{HTTPS} off
# Redirect to the secure version
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

# Normal redirect rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L] 

Thank you @naboovalley!

1

I had to adapt the above slightly to play nicely. Hopefully this saves someone some head banging.

<IfModule mod_rewrite.c>
 RewriteEngine On

 # If the request is not secure
 RewriteCond %{ENV:HTTPS} !=on
 # Redirect to the secure version
 RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

 # Send would-be 404 requests to Craft
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
 RewriteRule (.+) index.php?p=$1 [QSA,L]

</IfModule>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.