【问题标题】:How to forbid access to all directories except one using web.config in IIS8?如何禁止访问除在 IIS8 中使用 web.config 的目录之外的所有目录?
【发布时间】:2017-01-23 16:32:12
【问题描述】:

我基于Yii2开发了一个web项目,所以需要禁止访问除子目录“web”之外的所有文件和目录。

下面是项目目录结构:

.htaccess
composer.json
config/
    console.php
    web.php
commands/
controllers/
models/
runtime/
vendor/
views/
web/
    .htaccess   
    assets/         
    index.php

我在apache下通过.htaccess实现了这个,非常非常简单^_^

.htaccess

Order Allow,Deny
Deny from all

web/.htaccess

Order Allow,Deny
Allow from All

但是我现在需要在IIS8下部署这个项目,所以我尝试使用IIS管理将.htaccess转换为web.config,但转换结果为空。 我怎样才能做到这一点?谢谢

【问题讨论】:

    标签: .htaccess iis url-rewriting yii2 web-config


    【解决方案1】:

    IIS 使用请求过滤模块来限制浏览器对作为应用程序组件的文件的访问。对于 Web.config 文件中的示例应用程序,该部分可能如下所示:

    <security>
            <requestFiltering>
                <denyUrlSequences>
                    <add sequence="engine" />
                    <add sequence="inc" />
                    <add sequence="info" />
                    <add sequence="install" />
                    <add sequence="module" />
                    <add sequence="profile" />
                    <add sequence="po" />
                    <add sequence="sh" />
                    <add sequence="theme" />
                    <add sequence="tpl(\.php" />
                    <add sequence="Root" />
                    <add sequence="Tag" />
                    <add sequence="Template" />
                    <add sequence="Repository" />
                    <add sequence="code-style" />
                </denyUrlSequences>
                <fileExtensions>
                    <add fileExtension=".sql" allowed="false" />
                    <add fileExtension=".pl" allowed="false" />
                </fileExtensions>
            </requestFiltering>
        </security>
    

    请注意,您可以将此部分注释掉以进行安装,因为此过滤器会阻止安装脚本。

    使用请求过滤的替代方法是使用 URL 重写器模块为任何匹配的文件类型返回 403 错误。 URL Rewriter 模块的优点是它使用正则表达式进行匹配。

            <rule name="Protect files and directories from prying eyes" stopProcessing="true"> 
                <match url="\.(engine|inc|info|install|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl|svn-base)$|^(code-style\.pl|Entries.*|Repository|Root|Tag|Template|all-wcprops|entries|format)$" /> 
                <action type="CustomResponse" statusCode="403" subStatusCode="0" 
                    statusReason="Forbidden" 
                    statusDescription="Access is forbidden." /> 
            </rule>
    

    【讨论】:

    • 我的目的是禁止目录访问(包括所有子目录和子目录中的文件),而不是限制目录索引。
    • 它会拒绝访问文件夹和子文件夹,当有人试图访问它时会抛出 403 异常。
    • 不能禁止访问文件。例如,www.example.com/web/config.php
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多