0

I recently installed my SSL certificate, and I'm attempting to enforce a https connection to all my pages. However, previously I also redirected all requests to the www version of the request page. When combining an http redirect to https and concurrently redirecting traffic to www, I get a looping redirect warning on browsers. Hence, how can I make .htcaccess rule (I actually just use the directory config file) that will achieve what i want: always https://'www'

Here's the current combination that I have:

RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}$1 [NC,R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule (.*) https://www.mydomain.com$1 [R=301,L]
user3718546
  • 323
  • 4
  • 12
  • Actually, can somebody explain fully why not to use 'www' and why use it? Stackoverflow prevented me from linking to url with 'www' in it, hence why I'm asking. Either way, I still want to redirect all my traffic to https:// or https://'www' [I put the quotes so StackOverflow will allow me to put the www] – user3718546 Sep 10 '14 at 00:20

1 Answers1

0

Your question is a duplicate of htaccess redirect to https://www

Also, you can solve the WWW problem with DNS by simply pointing your naked domain to WWWizer's free naked domain redirect service's IP 174.129.25.170

Community
  • 1
  • 1
iSWORD
  • 768
  • 15
  • 22
  • I had some odd results with that solution, I would get redirect loops in some browsers and nothing in others. I can't figure out why this is so hard! Facebook seems to be able to redirect everything the way I'm asking. – user3718546 Sep 10 '14 at 01:22
  • @user3718546 you can always do it with php if you want to make sure it works all the time, something like `if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'off') { header ('Location: https://www.example.com'); }` – iSWORD Sep 10 '14 at 01:29
  • @user3718546 extend that for www and the condition will be `if ( empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'off' || substr($_SERVER['SERVER_NAME'], 0, 3) != 'www' )` – iSWORD Sep 10 '14 at 01:37
  • Yuup that works. I was doing it for static site so I ended up just doing it the nginx route (my server uses both Apache and Nginx). I'd do it with PHP but I rather make my files cacheable whenever I can. – user3718546 Sep 11 '14 at 03:42