1

I'm a beginner when it comes to doing stuff with htaccess so please be patient with my dumb question. I know that this has been addressed a lot (here, here, and here for example) but it doesn't seem to work for my situation. My browser displays a "redirect loop" error with this code in my htaccess file:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Trying to redirect to https
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>

I know that this probably has something to do with the RewriteCond and the RewriteRule before I redirect to https, but I don't really know what I'm doing here and I don't know what to change.

Update:

Some more info that might be helpful:

  1. When I remove the "redirect to https" code and manually type https://my.site.com it loads just fine.
  2. Also, the redirect-to-https thing worked great before I accidentally deleted the .htaccess file in my directory.
  3. The app that I'm trying to redirect resides in a subfolder of another app which also has an .htaccess file. Here's the code for that app:

    <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] RewriteEngine On </IfModule>

This is what firebug says: 1st screenshot2nd screenshot

Community
  • 1
  • 1
sterfry68
  • 1,063
  • 2
  • 14
  • 30

1 Answers1

1

Change order of your rules and keep 301 rules before other internal rewrite rule:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Trying to redirect to https
    RewriteCond %{HTTPS} off
    RewriteCond %{SERVER_PORT} !=443
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

</IfModule>
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • still get "This webpage has a redirect loop" – sterfry68 Jul 02 '14 at 20:39
  • Test in a different browser. – anubhava Jul 02 '14 at 20:45
  • Just tested in Firefox and got a similar message: "Firefox has detected that the server is redirecting the request for this address in a way that will never complete." – sterfry68 Jul 02 '14 at 20:51
  • If I remove the `[L,NE,R=301]` line it loads OK, but it doesn't redirect to https: and none of my styles, images, js load. – sterfry68 Jul 02 '14 at 20:52
  • I'm using laravel (which is a php framework, not a CMS). Do you think it could be a problem with my hosting provider? – sterfry68 Jul 02 '14 at 20:55
  • It can very well be. Laravel OR some other rule is forcing back `http://` and our rule is redirecting to `https://` causing looping. – anubhava Jul 02 '14 at 20:57
  • Don't think that Laravel is doing that but I'll check. Also check with my hosting provider. thanks. – sterfry68 Jul 02 '14 at 21:03
  • I don't think that anything outside of this file is causing the redirect loop error. When I comment out the "redirect to https" lines and type `https://my.site.com` the page loads just fine. Also, this https redirect was working fine before. I just happened to delete my .htaccess file and now I've got to try and reproduce it. – sterfry68 Jul 09 '14 at 18:09
  • Open firebug and then visit your site. Check `Net` tab then to see wha t you get there. – anubhava Jul 09 '14 at 18:20
  • added some screenshots. not sure how to read this except that yes, it's definitely being redirected over and over. – sterfry68 Jul 09 '14 at 18:44
  • Try updated code now. If it still causes looping try commenting out `https` rule and see if that changes anything. – anubhava Jul 09 '14 at 18:56
  • Thank you. That seems to be working. Could you explain to me what's going on here? Why was changing the port the solution? And how did you identify this as the solution? – sterfry68 Jul 09 '14 at 19:21
  • I have faced this situation while answering couple of question on SO in the past that their Apache wasn't setting `%{HTTPS}` variable correctly. So even if it is `https://` URI the variable `%{HTTPS}` was not set. I could see from your screenshot that same redirection is happening over and over again so I suggested using PORT which is definitely set to correct value i.e. 443 for `https` – anubhava Jul 09 '14 at 19:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57050/discussion-between-sterfry68-and-anubhava). – sterfry68 Jul 09 '14 at 19:29