【问题标题】:.Net Core / IIS web config rewrite rules.Net Core / IIS web config 重写规则
【发布时间】:2018-03-26 01:45:33
【问题描述】:

在我的情况下,我有两个域:

domain.com
domain1.com

其中domain1.comdomain.com 的别名

对于domain1.com,我只想允许以api/ 开头的网址的流量,并将所有其他流量重定向到具有301 状态代码的domain.com。

即我想允许像domain1.com/api/products/list 这样的请求另一方面我想将domain1.com/products/list 重定向到domain.com

谁能指导如何使用 web.config 实现这一目标?

【问题讨论】:

    标签: asp.net-core web-config url-rewrite-module asp.net-core-2.0 iis-10


    【解决方案1】:

    遵循 URL 重定向规则应该可以工作。

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="Domain1">
                        <match url="^(.+)" />
                        <conditions>
                            <add input="{HTTP_URL}" pattern="^/api/(.+)" negate="true" /> 
                        </conditions>
                        <action type="Redirect" url="http://domain/{R:1}" redirectType="Permanent" appendQueryString="true" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>
    

    快速解释。

    • 如果需要,我们会使用 match 模式捕获整个传入 URL,以便对重定向进行反向引用。
    • conditions 中,我们否定了API URL 模式,因此该规则适用于 包含/api 的URL。
    • 重定向操作采用捕获的反向引用并将其附加到域。

    注释。

    • 您可能需要调整规则中的斜线,我假设/api 之后总会有一个斜线。
    • 浏览器大量缓存永久重定向,确保每次修改规则时使用隐身/私人会话。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-12
      • 2020-12-20
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      • 2014-10-27
      相关资源
      最近更新 更多