Apache server redirecting to https when it shouldn't

Viewed 13

I made a .htaccess to redirect http requests to https. However my local testing server does not support https so I need to redirect only when the hostname does not match a given one:

RewriteCond %{HTTPS} != on
RewriteCond %{HTTP_HOST} != localhost [NC]

RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]

The problem is that it redirects to https in every situations. Do you have any idea on why it acts like this ?

1 Answers
RewriteCond %{HTTPS} != on
RewriteCond %{HTTP_HOST} != localhost [NC]

There should be no space after the = operator. (Although I would have expected this to have resulted in an error?) It should be written like this:

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !=localhost [NC]

In this case !=on and !=localhost are a single argument. The = and ! operators (yes, two operators, not one) are part of the argument itself.

You are also missing the L flag on the RewriteRule. Not an issue if this is the only rule you have, but otherwise it is required to prevent further processing.

You will also need to clear your browser cache as the erroneous will have been cached by the browser.

Related