I write the following .htaccess
rules to redirect all URLs from www.a.com
to www.b.com
:
RewriteEngine on
RewriteCond %{http_host} ^www.a.com [NC,OR]
RewriteCond %{http_host} ^a.com [NC]
RewriteRule ^(.*)$ https://www.b.com/myfolder/ [R=301,NC,L]
Now there is a file under a subdir of a.com
, as below:
www.a.com/.well-known/acme-challenge/#######
It is used for AutoSSL renewal. With the above redirection, the AutoSSL will not be able to find the file any more, so fail the renewal.
So, is it possible to disable the redirect under /.well-known/acme-challenge/
?
Since you are redirecting everything to a fixed URL-path you might as well build the exception directly in the
RewriteRule
directive (there is no need to capture the URL-path as you are currently doing).For example:
The
!
prefix on theRewriteRule
pattern negates the expression, so it is successful when the regex does not match. In this case when the requested URL-path does not start.well-known/acme-challenge/
. (However, see the "edge case" below.)I also combined your two conditions (
RewriteCond
directives) into one. There is no need for theNC
flag on the rule itself (assuming you are on Linux, not Windows).Remember to backslash-escape literal dots in the regex.
Edge case (Optional extra)
Note that if you also have a front-controller pattern later in the
.htaccess
file (eg. WordPress, Joomla, most other frameworks/CMS, etc.) then you should also add a condition to the above rule to prevent rewrites to the front-controller being redirected when requesting a URL of the form/.well-known/acme-challenge/<non-existent-file>
. For example:Without this condition then a request for
http://a.com/.well-known/acme-challenge/<non-existent-file>
would otherwise be redirected tohttps://www.b.com/index.php
(assumingindex.php
is the front-controller).Summary
The complete rule would then become:
UPDATE:
Since the simple
Redirect
(mod_alias) directive itself does not provide the ability to make "exceptions", you would need to surround the directive in an<If>
expression (requires Apache 2.4). For example:Reference: https://httpd.apache.org/docs/2.4/expr.html
(If you are using the
Redirect
directive like this then there is no "front-controller" pattern and no "edge case" as mentioned above.)Aside: Whilst they might look similar, this
Redirect
directive is quite different to theRewriteRule
directive used earlier and produces different results. TheRewriteRule
directive above discards the requested URL-path, redirecting everything tohttps://www.b.com/myfolder/
. However, theRedirect
directive used here preserves the requested URL-path, prefixing the URL-path with/myfolder
(eg./foo/bar
is redirected tohttps://www.b.com/myfolder/foo/bar
)