2

Background

I've managed to get http to https redirection working for my Wordpress installation by including this snippet in my .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

I'm running in the Google Cloud environment and the problem is that the health check requires that the "health checked resource" returns 200 OK. The health check issues an HTTP request without the X-Forwarded-Proto header set so Apache will respond with a 301 and thus the health check fails (it doesn't follow redirects). Unfortunately it's not possible to define headers in the Google Health Checker so I can't fake the X-Forwarded-Proto header to "https". So my hope for a workaround would be to supply a query parameter in the health check path and configure the .htaccess file to ONLY redirect if the specified query param is undefined (or equal to false or something similar).

Question

How can I modify the RewriteCond (I presume) to only perform redirection if the X-Forwarded-Proto header is not equal https (as in the example above) AND the query string does not contain the parameter healthCheck=true?

1 Answer 1

2

Can you try with this?

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{QUERY_STRING} !.*healthCheck=true.*
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

Take a look at https://stackoverflow.com/questions/4824964/rewritecond-in-htaccess-with-negated-regex-condition-doesnt-work and https://wiki.apache.org/httpd/RewriteQueryString .

2
  • This works but note that the ".htaccess file" I presented in my question was slightly wrong (I've now edited it). The "RewriteRule" should look like this: RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    – Johan
    Dec 11, 2015 at 7:32
  • 1
    Corrected answer with new RewriteRule.
    – emi
    Dec 11, 2015 at 11:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .