3

I have taken over a website and am trying to force https. I have added the following to my .htaccess file:

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L]

The rest of the file looks like this:

RewriteCond %{REQUEST_FILENAME} !^(.*)/audio_recording/(.*)$
RewriteCond %{REQUEST_FILENAME} !^(.*)/bwcheck/(.*)$
RewriteCond %{REQUEST_FILENAME} !^(.+)/page\.php$
... lots of Conditions
RewriteRule ^(.*)\.* page.php?$1 [L]
IndexIgnore *

When I add the https redirect I am getting the following error:

The page isn't redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

and chrome:

This webpage has a redirect loop...

The url is updated to https however.

EDIT:

Here is the begining of the .htaccess file:

php_value memory_limit 64M

<Files .htaccess>
order allow,deny
deny from all
</Files>

DirectoryIndex index
Options +FollowSymlinks
RewriteEngine on
RewriteBase /

ErrorDocument 404 /web/content/content/404.php
Shawn Northrop
  • 5,826
  • 6
  • 42
  • 80
  • Send a https request to your webssite and see what happens? – anubhava Jul 01 '13 at 17:35
  • I get the same error regardless of requesting http or https – Shawn Northrop Jul 01 '13 at 17:38
  • 1
    So even with 2 lines commented as shown above when you get HTTPS request you still get infinite looping? – anubhava Jul 01 '13 at 18:54
  • Sorry the comments are not in the file, i had those in while testing. When commented out there is no infinite loop... I will update the question to reflect this. – Shawn Northrop Jul 01 '13 at 21:01
  • Also, if i remove the second rewrite: `RewriteRule ^(.*)\.* page.php?$1 [L]` I do not get the infinite loop. It seems its just the combination of the two – Shawn Northrop Jul 01 '13 at 21:03
  • I just realized that my last comment was untrue. If I remove the second rewrite rule I still get the redirect error. I have changed the first rewrite rule to the following: `RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}/test/ [L,R]` when loading the page I get the following URL `https://www.yogalivelink.com/test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test/` It appears that it does not recognize https as being `on` – Shawn Northrop Jul 03 '13 at 18:44
  • Step 1: Rename your .htaccess to something else. Step 2: Try opening this link: `https://www.yogalivelink.com/` and see what happens. – anubhava Jul 03 '13 at 18:52
  • That seems to load the site just fine, with a lock in the url bar. – Shawn Northrop Jul 03 '13 at 19:02
  • Ok now have .htaccess back with just first https rule. And try both: `http://www.yogalivelink.com/` and `https://www.yogalivelink.com/` – anubhava Jul 03 '13 at 19:05
  • both `http` and `https` redirect me to `https://www.yogalivelink.com/test//test//test//test//........` – Shawn Northrop Jul 03 '13 at 19:06
  • Its acting like `RewriteCond %{HTTPS} !=on` is returning true even when the url is `https` – Shawn Northrop Jul 03 '13 at 19:08
  • Ok replace that `RewriteCond %{HTTPS} !=on` line with: `RewriteCond %{SERVER_PORT} ^80$` – anubhava Jul 03 '13 at 19:09
  • That seemed to work if I remove the second rewrite. If the second is still there I get the same error as before – Shawn Northrop Jul 03 '13 at 19:23
  • You mean commenting `RewriteRule ^(.*)\.* page.php?$1 [L]` makes it work? – anubhava Jul 03 '13 at 19:29
  • yes, i removed that rule and all of its conditions leaving only: `RewriteCond %{HTTPS} !=on RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}/test/ [L,R]` – Shawn Northrop Jul 03 '13 at 19:32
  • Ok with that still commented try loading: `https://www.yogalivelink.com/page.php?something` – anubhava Jul 03 '13 at 19:35
  • Sorry scratch that it seems to still be in a loop. It appeared to be working for a minute in firefox but after trying in chrome and opening a new window in ff it seems it is still in a loop. That is with or without the second condition in the file using `RewriteCond %{SERVER_PORT} ^80$` – Shawn Northrop Jul 03 '13 at 19:36
  • Sorry it seems to a localized problem that I wont be able to debug from comments only. – anubhava Jul 03 '13 at 19:55
  • Ok thanks for trying. Ill update if I end up figuring this one out – Shawn Northrop Jul 03 '13 at 20:04
  • Below link helped me out resolve the same issue (: http://stackoverflow.com/a/21754308/5465790 – Walk Apr 06 '17 at 10:24

3 Answers3

6

The following seemed to fix my problem:

RewriteCond %{ENV:HTTPS} !on [NC]

Shawn Northrop
  • 5,826
  • 6
  • 42
  • 80
0

its as simple as:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Ketan Savaliya
  • 1,190
  • 9
  • 11
  • 1
    As a rule of thumb, answers should be more verbose than the problem statements they address. Some discussion (e.g. pros and cons of a redirect strategy) is especially appropriate in responding to such a very old (4+ years) Question with an Accepted Answer. In particular you likely will need to highlight what in your solution is new information or benefit. – hardmath Dec 09 '17 at 18:49
0

Try This One

This gonna fix your problem.

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://yourwebsite.com/$1 [R,L]
  1. http ==> https

  2. www ==> https

  3. http ==> https

  4. http://www ==> https