【问题标题】:IIS Rewrite Rule - How to include root documentsIIS 重写规则 - 如何包含根文档
【发布时间】:2015-11-07 09:08:09
【问题描述】:

我有一个简单的 IIS 规则将 HTTPS 重定向到 HTTP:

<rule name="HTTPS" enabled="true" stopProcessing="true">
    <match url=".*\.(asp)$" ignoreCase="false" />
    <conditions>
        <add input="{HTTPS}" pattern="on" />
        <add input="{REQUEST_URI}" negate="true" pattern="^/ecards/user*" ignoreCase="true" />
    </conditions>
    <action type="Redirect" url="http://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>

匹配的 url 仅适用于 .asp 文件,但我怎样才能让它也适用于根目录?

例如

example.com
example.com/
example.com/test
example.com/test/

我不想只将匹配 URL 设为:

<match url="(.*)" />

因为其他非.asp 文件会被重写。

【问题讨论】:

    标签: asp.net iis url-rewriting


    【解决方案1】:

    在规则中包含目录的一种方法是排除其他所有内容。

    假设 其他所有内容 具有文件扩展名(例如 .php.css.js 等),您可以否定路径中包含 . 的所有输入。

    我稍微更改了您的代码以在本地制作一个有效的演示(我没有在本地测试 HTTPS,因此我没有重定向到 HTTP,而是将其设置为重定向到 About.aspx),两条规则是:

    <rule name="HTTPS" enabled="true" stopProcessing="true">
        <match url=".*\.(asp)$" ignoreCase="false" />
        <action type="Redirect" url="/About.aspx" appendQueryString="true" redirectType="Permanent" />
    </rule>
    <rule name="NEWRULE" enabled="true" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
            <add input="{REQUEST_URI}" negate="true" pattern=".*\.(.)$" ignoreCase="true" />
            <add input="{REQUEST_URI}" negate="true" pattern="^/About*" ignoreCase="true" />
        </conditions>
        <action type="Redirect" url="/About.aspx" appendQueryString="true" redirectType="Permanent" />
    </rule> 
    

    因此,根据您的原始代码示例,适用于您的新规则将类似于:

    <rule name="IncludeDirectories" enabled="true" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
            <add input="{REQUEST_URI}" negate="true" pattern=".*\.(.)$" ignoreCase="true" />
            <add input="{HTTPS}" pattern="on" />
            <add input="{REQUEST_URI}" negate="true" pattern="^/ecards/user*" ignoreCase="true" />
        </conditions>
        <action type="Redirect" url="http://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>  
    

    注意:上述方法比较激进。你可以替换这个条件:

    <add input="{REQUEST_URI}" negate="true" pattern=".*\.(.)$" ignoreCase="true" />
    

    以下内容:

    <add input="{REQUEST_URI}" negate="true" pattern=".*\.(php|css|js|jpg|gif|png)$" ignoreCase="true" />
    

    排除特定扩展名的地方。您想添加多少就添加多少。

    编辑:如果您想让特定页面仍然使用 HTTPS,那么以下规则可能会有所帮助(虽然尚未测试)。上一条规则将所有 URL 发送到 HTTP,除了那些具有 /ecards/user 的 URL,此规则将具有 /ecards/user 的 URL 发送到 HTTPS。我相信不会有冲突。

    <rule name="HTTPS2Admins" enabled="true" stopProcessing="true">
        <match url="/ecards/user(.*)" />
        <conditions>
            <add input="{HTTP}" pattern="on" />
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}/ecards/user{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
    

    【讨论】:

    • 出于兴趣@Tasos K.,我正在尝试添加另一个规则来强制例如example.com/ecards/user* 到 https,但我一直卡在重定向循环中。那可能吗?您提供的规则将强制除 ecards/user* 之外的所有 HTTPS 到 HTTP,但我还需要强制 example.com/ecards/user* 到 HTTPS。
    • 条件&lt;add input="{REQUEST_URI}" negate="true" pattern="^/ecards/user*" ignoreCase="true" /&gt; 应该可以防止这种情况,对吧?我在我的演示中添加了这条规则,并且不会重定向 URL http://localhost:51801/ecards/user/dddhttp://localhost:51801/ecards/user-something。也许你的 web.config 中的另一个规则导致了这个问题。
    • 谢谢@Tasos K。我遇到的问题是,在我的重定向正常工作之前,我的整个网站都被谷歌索引为 HTTPS。现在我需要将所有 HTTPS 重定向到 HTTP,除了 ecard/user*,现在使用您的规则可以正常工作。但是如果用户访问 HTTP..ecard/user* 我需要重定向到 HTTPS,但只有那些页面,而不是其他任何东西。例如此jimpix.co.uk/ecards/user-sign-up.asp 需要通过不同的规则重定向到 https 版本,同时保留您的 HTTPS 到 HTTP 规则。
    • 关于 Google 及其索引,请考虑使用元 &lt;link rel="canonical" href="..." /&gt; 并将 HTTP 链接放在那里(请参阅 here)。几周后,Google 索引将使用您提供的链接。同样对于 CSS 和 JS 文件,使用 // 前缀而不是 http://https://(参见 here)。这些提示不适用于 IIS 重写规则,但它们可能会帮助您以更少的重写规则解决问题。
    • 谢谢,我会检查索引问题的规范选项。关于重写规则,有什么方法可以共存 2 个规则,一个用于 HTTPS 到 HTTP,如您所写,另一个用于同时强制从 HTTP 重定向到 HTTPS 一段时间目录,因为还有其他我网站上的文件夹我想强制使用 HTTPS,例如admin 文件夹等,如 jimpix.co.uk/admin/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 2018-02-12
    • 2011-03-03
    • 1970-01-01
    • 2014-05-01
    • 2014-10-27
    相关资源
    最近更新 更多