1

I know this question has been asked and answered many times but I've spent the last 3 hours going through peoples answers and none have seemed to work just right for me.

I need some .htaccess code to do this:

If domain is example.co.uk/$urlData (only apply to main domain and no subdomains) rewrite too www.example.co.uk/$urlData

If HTTPS is not "on" and domain is primary (ie. no subdomains) then redirect to https://www.example.co.uk/$urlData.

I have to use RewriteCond %{HTTP:X-Forwarded-Proto} !https to test if HTTPS is off as RewriteCond %{HTTPS} off is not configured on my server.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
Jack Trowbridge
  • 3,175
  • 9
  • 32
  • 56

2 Answers2

1

Give this a try and see if this will work for you.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*) https://www.example.com/$1 [R=301,L]
MrWhite
  • 43,179
  • 8
  • 60
  • 84
Panama Jack
  • 24,158
  • 10
  • 63
  • 95
  • Thank you, but I've just uploaded it and get an internal server error – Jack Trowbridge Jan 23 '15 at 22:59
  • @JackTrowbridge Yes I had a typo in the last line. It was supposed to be `RewriteRule` I guess I was typing too fast. – Panama Jack Jan 26 '15 at 16:44
  • @JackTrowbridge The way I have it is less code and accomplishes what you needed. Sorry about the typo which I corrected and will leave my answer if someone else wants to use it. – Panama Jack Jan 26 '15 at 16:51
1

Add this to your .htaccess:

RewriteEngine On

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

RewriteCond %{HTTP_HOST} ^www\.example\.co\uk$ [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule (.*) https://www.example.co.uk/$1 [R=301,L]
MrWhite
  • 43,179
  • 8
  • 60
  • 84
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89