# Redirect to HTTPS except localhost

**URL:** <https://community.letsencrypt.org/t/redirect-to-https-except-localhost/238602>\
**Category:** Help\
**Created:** [June 18, 2025, 8:45am UTC](https://community.letsencrypt.org/t/redirect-to-https-except-localhost/238602 "2025-06-18T08:45:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![alfredino](https://avatars.discourse-cdn.com/v4/letter/a/b5e925/32.png) [@alfredino](https://community.letsencrypt.org/u/alfredino)\
**Post date:** [June 18, 2025, 8:45am UTC](https://community.letsencrypt.org/t/redirect-to-https-except-localhost/238602/1 "2025-06-18T08:45:50Z")

</div>

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](https://community.letsencrypt.org/t/apache-redirect-everything-except-acme-challenge-requests-example/197848/7) and [this](https://httpd.apache.org/docs/2.4/expr.html) since I am using the IF-ELSE directive.

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

```

How can I add localhost?  
Thank you

---

<div class="post-metadata">

**Author:** ![Osiris](https://avatars.discourse-cdn.com/v4/letter/o/839c29/32.png) [@Osiris](https://community.letsencrypt.org/u/Osiris)\
**Post date:** [June 18, 2025, 11:05am UTC](https://community.letsencrypt.org/t/redirect-to-https-except-localhost/238602/2 "2025-06-18T11:05:27Z")

</div>

According to [Expressions in Apache HTTP Server - Apache HTTP Server Version 2.4](https://httpd.apache.org/docs/2.4/expr.html) 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 HTTP **S** 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.

---

<div class="post-metadata">

**Author:** ![alfredino](https://avatars.discourse-cdn.com/v4/letter/a/b5e925/32.png) [@alfredino](https://community.letsencrypt.org/u/alfredino)\
**Post date:** [June 18, 2025, 11:29am UTC](https://community.letsencrypt.org/t/redirect-to-https-except-localhost/238602/3 "2025-06-18T11:29:34Z")

</div>

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](https://community.letsencrypt.org/t/apache-redirect-everything-except-acme-challenge-requests-example/197848/7) 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/](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

---

<div class="post-metadata">

**Author:** ![Osiris](https://avatars.discourse-cdn.com/v4/letter/o/839c29/32.png) [@Osiris](https://community.letsencrypt.org/u/Osiris)\
**Post date:** [June 18, 2025, 11:31am UTC](https://community.letsencrypt.org/t/redirect-to-https-except-localhost/238602/4 "2025-06-18T11:31:46Z")

</div>

> [@alfredino](#):
>
> Do you think I should ask in a different forum?

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

> [@alfredino](#):
>
> Do you know if the above does what I want?

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.

---

<div class="post-metadata">

**Author:** ![alfredino](https://avatars.discourse-cdn.com/v4/letter/a/b5e925/32.png) [@alfredino](https://community.letsencrypt.org/u/alfredino)\
**Post date:** [June 18, 2025, 11:38am UTC](https://community.letsencrypt.org/t/redirect-to-https-except-localhost/238602/5 "2025-06-18T11:38:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MikeMcQ](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/mikemcq/32/52772_2.png) [@MikeMcQ](https://community.letsencrypt.org/u/MikeMcQ)\
**Post date:** [June 18, 2025, 11:55am UTC](https://community.letsencrypt.org/t/redirect-to-https-except-localhost/238602/6 "2025-06-18T11:55:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alfredino](https://avatars.discourse-cdn.com/v4/letter/a/b5e925/32.png) [@alfredino](https://community.letsencrypt.org/u/alfredino)\
**Post date:** [June 18, 2025, 12:48pm UTC](https://community.letsencrypt.org/t/redirect-to-https-except-localhost/238602/7 "2025-06-18T12:48:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/letsencrypt/original/3X/c/a/ca6c06ea1ea201324bba7048c6841ce60236468d.png) [@system](https://community.letsencrypt.org/u/system)\
**Post date:** [July 18, 2025, 12:49pm UTC](https://community.letsencrypt.org/t/redirect-to-https-except-localhost/238602/8 "2025-07-18T12:49:30Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
