apache2.4 mod_rewrite exclude specific alias directroy/uri

18,096

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]

Solution 3

IMHO shorter and more robust:

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

you may want to add /acme-challenge/ eventually but if you would like to debug it with an arbitrary file, like ./well-known/test this solution works better

what does it actually do: looks whether the request starts with .well-known, in that case does nothing (the meaning of -) and make it the last rule [L]

Share:
18,096

Related videos on Youtube

FleBeling
Author by

FleBeling

Updated on September 18, 2022

Comments

  • FleBeling
    FleBeling almost 2 years

    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?

  • FleBeling
    FleBeling over 8 years
    First, thanks for you advice! Unfortunately, it didn't work. When you visit: http://www.server-plant.de/.well-known/acme-challenge/it is still being rewritten. (I applied the same Rewrrite Rules and Conditions for my www-Subdomain, so it's excatly the same as the cloud-Subdomain)
  • Froggiz
    Froggiz over 8 years
    Edited : there was () instead of {} and RewriteCond %{HTTPS} off is not requiered
  • FleBeling
    FleBeling over 8 years
    Yeah, I just blindly copied yours without checking for the right brackets, but well, sh.. happens :) Now it works like charm and you're also right regarding the other condition. Just one question left: How to change the condition to also match everything behind the trailing /? So it wouldn't catch index.html and so on?
  • Froggiz
    Froggiz over 8 years
    it already match all start with /.well-known/acme-challenge/that mean /.well-known/acme-challenge/anything will not be redirect too
  • FleBeling
    FleBeling over 8 years
    Okay, it seems there was something in my browser's cache or so. Now at home it works out of the box. And again, thanks alot!
  • Frederick Nord
    Frederick Nord over 7 years
    nope. The virtualhosts still seem to have precedence. "Sections inside <VirtualHost> sections are applied after the corresponding sections outside the virtual host definition. This allows virtual hosts to override the main server configuration." from httpd.apache.org/docs/current/sections.html
  • Frederick Nord
    Frederick Nord over 7 years
    "Note that rewrite configurations are not inherited by virtual hosts. This means that you need to have a RewriteEngine on directive for each virtual host in which you wish to use rewrite rules." httpd.apache.org/docs/2.4/mod/mod_rewrite.html
  • tobltobs
    tobltobs about 7 years
    Rewrite configurations can be inherited to all virtual hosts with RewriteOptions InheritDown since Apache 2.4.8. RewriteEngine On has still to be present in each virtual host configuration.
  • James Tan
    James Tan about 5 years
    adding this to stop the ssl redirect rules below it is awesome
  • Jette
    Jette about 5 years
    The single dot in line 2 did not work for me. Is it because it only matches one char and the url consists of several chars? I am using ^(.*)$ instead.