1

I have a problem with my .htaccess and https redirection.

First of all, I have consulted these links to find a solution but none could help me.

List of links :

When I load the page with http, the .htaccess redirect me to https but when I load the page with https, I have an infinite loop.

Here is my .htaccess code :

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

RewriteCond %{HTTP_HOST} ^(api|www|dev|stefano|sav)\.
RewriteCond %{REQUEST_URI} !^/(api|www|dev|stefano|sav)/
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^(.*)$ %1/$1 [L]

Is anybody can help me create a redirect condition (http to https) ?

Community
  • 1
  • 1
SatanicGeek
  • 342
  • 5
  • 15

2 Answers2

7

You are leaving off the Rewrite Flags You need to tell it force the redirection with R flag and optional provide status code which is recommended 301, 302 etc.

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

By the way, every example you linked to shows the answer using the R flag, not sure why you didn't use exact examples :)

Solution if behind devices like load balancer.

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Panama Jack
  • 24,158
  • 10
  • 63
  • 95
  • It doesn't work with flags. I have an error (error 500) – SatanicGeek Jan 08 '16 at 09:22
  • There is another problem then. You should be using the flags. What does your apache error log say for the 500 error? – Panama Jack Jan 08 '16 at 09:23
  • Oh. I can't have access to the log file. I will talk with my IT. – SatanicGeek Jan 08 '16 at 09:24
  • 2
    Make sure you don't have the old cached. try the rules with the flags, but clear your browser cache and test it again or try in another browser. – Panama Jack Jan 08 '16 at 09:26
  • The IT department found the problem. This comes from the Apache ModRewrite module. It is apparently corrupt . They will reinstall the server and I will be able to test if it works. – SatanicGeek Jan 08 '16 at 09:30
  • It's working ... But not at 100%. When I want to load http://www.irat-tools.ch/, the ModRewrite redirect me to https://www.irat... but if I want to load http://sav.irat-tools.ch/, I have a redirect loop (same problem if I type https://irat-tools.ch/) – SatanicGeek Jan 08 '16 at 09:51
  • Ok, let's do this. You original problem is actually fixed now. Which is just http to https redirection. I would consider this a new problem. Now you if an issue with www redirection. It would be better to not trouble shoot the new issue in the comments. Can we solve this question and you create a new question with new issue. I can try and assist with that one too. Please put exactly the type of URL you are wanting to use and the expected outcome URL after redirection in the question. – Panama Jack Jan 08 '16 at 09:56
  • I found the answer : `RewriteCond %{HTTP:X-Forwarded-Proto} !https` – SatanicGeek Jan 08 '16 at 09:57
  • 1
    Ok perfect. you must be behind a load balancer then most likely. Glad you got it work. – Panama Jack Jan 08 '16 at 09:58
  • 1
    Thank you for your answer ;) – SatanicGeek Jan 08 '16 at 09:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100155/discussion-between-satanicgeek-and-panama-jack). – SatanicGeek Jan 08 '16 at 13:30
1

I have been fighting with this same problem for hours. It seems that I needed the solution for systems behind a load balancer. Perhaps this is because I am using Cloudflare with my site.

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

After adding the above lines to my .htaccess file, I deleted the cache on Cloudflare and the site loads as it should.

Frank C Jones
  • 87
  • 1
  • 4