IIS rewrite URL from .htaccess: match all files and folders starting with . and deny access

On  IIS and trying to translate into it the following Apache rule:

# prevent httpd from serving dotfiles (.htaccess, .svn, .git, etc.) 
# - except let's encrypt challenge
RedirectMatch 403 ^/?\.(?!/well-known/acme-challenge/[\w-]{43}$)

RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]

Open in new window


The idea of the rule is prevent IIS from serving:

    Any file than starts with “.”, or

    Any folder that starts with “.” but the folder “./well-known/acme-challenge”

Thanks in advance ;)
VP DDAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan McFaddenSystems EngineerCommented:
First, I'd like to see the how you would create a URL path with a "dot" as the first character.  To me, the uri-stem "./well-known/acme-challenge/*" seems to indicate that somewhere there is a directory named "." which is not allowed.

Or is there an application that utilizes URL routing rules (something like an MVC structure) that handles URL routing/mapping?

Not everything doable in Apache is doable in IIS and visa-versa.

Typically you can import the .htaccess rules using the URL Rewrite feature (if it is installed on the IIS Server).  The issue with the Apache rewrite rule is that is uses a flag that URL Rewrite does not support, that being the "[E=BASE:%1] statement on the rewrite.  Which is trying to dynamically update the CGI environment variable named BASE.

You can setup static CGI environment variables in IIS, but the URL Rewrite feature does not support an interface for setting/updating environment variables (for CGI or FastCGI).

So, unfortunately, this rule is not compatible with the URL Rewrite feature in IIS.

Dan
0
VP DDAuthor Commented:
Hi Dan,

Easily CMD and  md .demo

In the case of ./well-known/acme-challenge is created by a certifcate intaller (Certify).

Well, there is no way to deny files or folder staring with "." on IIS?

Thanks!
0
Dan McFaddenSystems EngineerCommented:
OK, something re-learned today.  Wasn't thinking of command line create.

Either way, the issue in the rule is not the dot in the path, it is the environment variable assignment in the htaccess rule.  There is no facility for IIS (no API available for IIS) to modify  the user environment variables.  Essentially making this rewrite rule incompatible with IIS URL Rewrite feature.

If you remove the line "RewriteRule ^(.*) - [E=BASE:%1]" you can import the htaccess file into the URL Rewrite feature directly.

Dan
0
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

VP DDAuthor Commented:
Maybe  [E=BASE:%1] cannot be translated to IIS

the 1st part
# prevent httpd from serving dotfiles (.htaccess, .svn, .git, etc.) 
# - except let's encrypt challenge
RedirectMatch 403 ^/?\.(?!/well-known/acme-challenge/[\w-]{43}$)

Open in new window


this will translated onto web.config as

<rule name="Deny files and folderd starting with . but allow folder .well-known" patternSyntax="ECMAScript" stopProcessing="true">
   <match url="(^\.|\/\.(?!well-known))" ignoreCase="true" negate="false" />
   <conditions logicalGrouping="MatchAll">
   </conditions>
   <action type="AbortRequest" />
</rule>

Open in new window

0
Dan McFaddenSystems EngineerCommented:
Which is what I had mentioned in my previous post.

Dan
0
VP DDAuthor Commented:
Sorry did not see it, where did you mention it?

If you are referring to the rule that is generated by importing the .htaccess into IIS is not even close to it.
If you import the above .htaccess to IIS you get this:
.htaccess to IIS y
0
Dan McFaddenSystems EngineerCommented:
I mentionedI mentioned it here.

If you remove the line "RewriteRule ^(.*) - [E=BASE:%1]" you can import the htaccess file into the URL Rewrite feature directly.

Dan
0
VP DDAuthor Commented:
Yes, it can be imported but it does not work.

The rule is what you see on above image without the red dots lines.

So, it does not work as the .htaccess it does not “match all files and folders starting with . and deny access” as asked.
0
Dan McFaddenSystems EngineerCommented:
Which is what i mentioned early on in my first post.


Typically you can import the .htaccess rules using the URL Rewrite feature (if it is installed on the IIS Server).  The issue with the Apache rewrite rule is that is uses a flag that URL Rewrite does not support, that being the "[E=BASE:%1] statement on the rewrite.  Which is trying to dynamically update the CGI environment variable named BASE.

You can setup static CGI environment variables in IIS, but the URL Rewrite feature does not support an interface for setting/updating environment variables (for CGI or FastCGI).

So, unfortunately, this rule is not compatible with the URL Rewrite feature in IIS.

There was no working solution given because the mod_rewrite rule is not compatible with IIS; URL Rewrite feature.  I thought I was being quite clear about the issue.

Dan
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft IIS Web Server

From novice to tech pro — start learning today.