Redirect to HTTPS except localhost

Hi,
I would like to modify my apache configuration file in order to redirect all traffic to HTTPS except if it comes from localhost (to access for instance to phpMyAdmin).
I already read this and this since I am using the IF-ELSE directive.

<If "%{REQUEST_URI} !~ m#/\.well-known/acme-challenge/#" | "%{HTTP_HOST} == 'localhost'">
        Redirect permanent / https://example.com/
</If>

How can I add localhost?
Thank you

According to Expressions in Apache HTTP Server - Apache HTTP Server Version 2.4 Apache uses || as "OR" instead of the | you're using now.

Also, you're matching if the HTTP host is equal to "localhost". Don't you mean "NOT equal to"? I.e., !=?

This is more "generic Apache configuration" than that it is related to Let's Encrypt or TLS by the way. The fact you're attempting not to redirect to HTTPS makes it a liiiiiitle bit related, but it's not much.. Also, you're not providing much debugging info. Do you get warnings? Errors? What works? What doesn't? On many generic computer communities your thread would probably be closed immediately.

3 Likes

Hi,
thank you for your fast reply. You are right OR is || as in C style and I forgot ! -> !=. I have not tried yet, since I do not know about apache syntax and after the server upgrade I found this discussion.
Do you think I should ask in a different forum?
<If "%{REQUEST_URI} !~ m#/.well-known/acme-challenge/#" || "%{HTTP_HOST} != 'localhost'">
Redirect permanent / https://example.com/

Do you know if the above does what I want? Redirect all traffic to HTTPS except ACME challenges and localhost?
Thank you

Maybe, or you just might have gotten a fix already.

I'm not knowledable about these Apache If-expressions. I just mentioned what I read in the documentation. I'm not going to learn these If-expressions just to see if this works, you need to test for yourself.

2 Likes

Thank you, I don't like the trial and error approach. I will keep you updated.

Why don't you make a new virtual host just for localhost? Then you can do in that virtual host whatever unique thing you want for that

3 Likes

It could be a good idea. I do not know what is the best solution.