1

I'm having a bit of trouble with my htaccess file when trying to redirect http to https. I've looked at other question on stack and i've tried to implement them to see if it will sort my problem but nothing has worked as of yet

I'm trying to do a redirect using htaccess to say that if its http:// then redirect to https://

Here is my original htaccess rule that i'm trying to modify for this to work with https

# Get rid of www.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

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

Can anyone shine a light on what I need to do for this to work, every attempt i've done seems to cause a server error

Regards

Luke

Luke O'Regan
  • 777
  • 1
  • 5
  • 11

1 Answers1

0

You need to add an [OR] flag in your condition:

# Get rid of www.
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.|)(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

This makes it so you get redirected if it's not https or if there's a www

Jon Lin
  • 142,182
  • 29
  • 220
  • 220