'add letsencrypt exception htaccess

My current htaccess looks like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ order/$1 [L]
</IfModule>

Meaning webpage loading default the /order content. Now I want to add exception for .well_known/acme-challenge/letsencrypt_11.... etc.

Could not get it work with the lines below:

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

RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ order/$1 [L]
</IfModule>

I tried to add:

RewriteRule ^/?.well_known/.+$ - [L]

as well. I even looked for all posts on so, not found any answer that fits my need..



Solution 1:[1]

RewriteRule ^/?.well_known/.+$ - [L]

Something like this is all you require. (But note that it should be well-known with a hyphen, not well_known with an underscore.) It must go before your existing directives. This does assume your .htaccess file is in the document root and that the URL you are creating an exception for is of the for example.com/.well-known/...

For example:

RewriteEngine On

# Exception
# If the URL starts "/.well-known/" then stop here.
RewriteRule ^\.well-known/ - [L]

# www to non-www redirect
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# HTTP to HTTPS redirect
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Front-controller
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ order/$1 [L]

Note that literal dots should be backslash-escaped in the regex.


Aside:

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

RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ order/$1 [L]
</IfModule>

This creates an exception on just the first rule (that removes the www subdomain). On any request that does not have the www subdomain (and on the redirected request), processing continues as normal through your other directives. So, requests for /.well-known/... will be processed by the second and third rules.

Following this method (using a negative match), you would need to add the exception before every RewriteRule directive.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1