1

I am trying to get a couple of directories to always be https and everything else be http

With the exception of images, css files and js files which should be on whatever the page is on.

So I created this htaccess rewrite:

RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^www\. RewriteRule ^(.*)$ https://server.com/$1 [R=301,L] RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule ^login/(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] RewriteRule \.(gif|jpe?g|png|css|js)$ - [NC,L] RewriteCond %{HTTP:X-Forwarded-Proto} https RewriteCond %{REQUEST_URI} !^login/(.*)$ RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

But when I go to /login I get an error

Firefox has detected that the server is redirecting the request for this address in a way that will never complete. 

What needs to change for this to work properly?

2
  • Is %{HTTP:X-Forwarded-Proto} supported on your Apache? Commented Sep 14, 2015 at 6:26
  • Yes, its running on a AWS Load Balanced Beanstalk, so I have to use that method for comparing. Commented Sep 14, 2015 at 6:32

2 Answers 2

1

Have your rules in this order:

RewriteEngine On RewriteBase / RewriteRule \.(gif|jpe?g|png|css|js)$ - [NC,L] RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteCond %{THE_REQUEST} /login/ [NC] RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NC] RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteCond %{THE_REQUEST} !/login/ [NC] RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NC] RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteCond %{THE_REQUEST} /login/ [NC] RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC] RewriteCond %{HTTP:X-Forwarded-Proto} https RewriteCond %{THE_REQUEST} !/login/ [NC] RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC] 
Sign up to request clarification or add additional context in comments.

Comments

0

%{HTTP_HOST} or %{REQUEST_URI} can't be used in the RewriteRule, only in the RewriteCond

You can use %1, %2 etc as a reference to the different matching parts of the condition tho. But the full Request URI can be solved by this in the rewrite rule:

RewriteRule (.*) http://some_host$1

So if the hostname is not dynamic, it's pretty easy to fix.

2 Comments

Hmm, I have tried that without much luck, however what I Can say is that I removed the last 3 lines and it worked for redirecting to https, but without those last 3 lines, it wont redirect the other way, so its seems the last 3 lines are causing some sort of issue.
Did you try RewriteCond %{HTTPS} on instead of RewriteCond %{HTTP:X-Forwarded-Proto} https?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.