【问题标题】:.NET: Redirect from domain and subfolder to new domain, retaining path and querystring, using httpredirect in web.config.NET:从域和子文件夹重定向到新域,保留路径和查询字符串,在 web.config 中使用 httpredirect
【发布时间】:2020-03-17 05:09:57
【问题描述】:

我正在尝试将域重定向设置为域迁移的一部分。我需要注意的是,我从中重定向的站点位于更广泛域的子文件夹中。我还需要保留其余的路径和查询字符串。

我需要在 web.config 中设置一个重定向来实现以下目标:

我试过了:

<httpRedirect enabled="true" destination="https://www.domain1.com$V$Q" httpResponseStatus="Temporary" exactDestination="true" />

但这不会删除“mysite”子文件夹。它重定向到https://www.domain2.com/mysite 而不是https://www.domain2.com

我也尝试过创建重写规则,但我无法确定有效的输入和正则表达式。

TIA。

【问题讨论】:

    标签: c# asp.net web-config http-redirect


    【解决方案1】:

    您可以在 URL 重写中遵循两条规则:

    <rule name="remove-my-site">
      <match url="^mysite/([/_0-9a-z-]+)"/>
      <action type="Rewrite" url="{R:1}"/>
    </rule>
    <rule name="domain1-to-domain2">
        <match url=".*" />
        <conditions logicalGrouping="MatchAny">
            <add input="{HTTP_HOST}" pattern="domain1.com" />
        </conditions>
        <action type="Redirect" url="http://www.domain2.com/{R:0}" />
    </rule>
    

    您不会使用 httpRedirect 来实现。它用于许多简单的重写。

    您还可以查看Application Request Routing 文档,了解如何使用 IIS 配置反向路由器。

    我们可能错过了这一步:

    启用反向代理功能

    【讨论】:

    • 发挥了作用。谢谢!编辑:这似乎不适用于我们的 api 端点的 httprequests。这给出了 404。
    • 你能再解释一下吗?你得到什么行为?可能是一些CORS问题吗?如果您的域是 A,并且您使用 XHR 重定向到 B,那么出现 CORS 错误是有意义的。
    • 如果我在浏览器地址栏中点击了诸如domain1.com/mysite/api/getData?inputParam=45 之类的端点,那么它会按预期重定向。但是,如果我通过邮递员或通过代码中的 HttpClient 请求访问同一端点,以模拟实际如何使用 api,则响应为 404 Not Found。
    • 尝试从您的 api 添加 Access-Control-Allow-Origin: * 响应标头。我的猜测是,这就是阻碍你的原因。
    • 我在端点上添加了 [EnableCors(origins: "", headers: "", methods: "*")] 并部署到 domain2 但这没有效果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多