apache2.4 mod_rewrite exclude specific alias directroy/uri

I have the following setup on one of my vhosts:

...<VirtualHost *:80>
    ServerName cloud.domain.de
    ServerAdmin [email protected]
    ServerSignature Off

    Alias "/.well-known/acme-challenge" "/var/www/domain.de/vh-www/htdocs/public/.well-known/acme-challenge"

    <Directory "/var/www/domain.de/vh-www/htdocs/public/.well-known/acme-challenge">
      Require all granted
      ForceType 'text/plain'
    </Directory>

    <ifmodule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %(REQUEST_URI) !/\.well\-known/acme\-challenge/?.*
      RewriteCond %{HTTPS} off
      # RewriteRule ^\.well-known/acme-challenge/([A-Za-z0-9-]+)/?$ - [L]
      RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    </ifmodule>...

What I want to achieve is, that mod_rewrite does not rewrite the URL when the url http://cloud.domain.de/.well-known/acme-challenge/ is accessed.

I already tried different approaches, one of them being the commented-out RewriteRule above, but nothing seems to work: the server rewrites it to https everytime.

When I disable the rewriting for testing purposes, I can access the alias URL just fine...

How do I achieve the specific URL not being rewritten?


Solution 1:

Like that :

<ifmodule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/\.well\-known/acme\-challenge/
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</ifmodule>

If the URI match start with /.well-known/acme-challenge/ the request will not be redirected

Solution 2:

@mark Correct version of the "shorter and more robust" variant:

RewriteCond %{REQUEST_URI} ^/\.well\-known
RewriteRule . - [L]