【问题标题】:URL Rewrite to a subdomainURL重写到子域
【发布时间】:2018-10-22 03:54:40
【问题描述】:

我正在使用 ASP.NET 的重写工具从 http 重定向到 hpps。我想重新路由到

https://services.net/ExitInterview/home/about

但目前它正在路由到

https://services.net/home/about

以下是我的重定向规则:

    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}"
          redirectType="Permanent" />
    </rule>`

我可以在规则字符串中将“HTTP_HOST”文本与硬编码文本混合吗?还是有别的办法?

我不想对 url 进行硬编码,因为它会随着本地、暂存和生产而变化。

【问题讨论】:

    标签: asp.net iis url-rewriting url-rewrite-module


    【解决方案1】:
    <rule name="HTTP to HTTPS redirect" ="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/ExitInterview/{R:1}"
          redirectType="Permanent" />
    </rule>
    

    试试这个

    【讨论】:

    • 这很容易。添加ExitInterview,我以为我很早就尝试过,但没有奏效。现在它确实有效。也许我上次没有刷新缓存。我刷新了缓存,这有效。谢谢!
    【解决方案2】:

    这应该可以满足您在 web.config 文件中从 HTTP 重定向到 HTTPS 的要求:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
    <system.web>
        <httpRuntime executionTimeout="180" />
    </system.web>
    <system.webServer>
        <httpErrors errorMode="Detailed" existingResponse="PassThrough" />
        <rewrite>
        <rules>
            <rule name="HTTP to HTTPS redirect" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="off" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
            </rule>
        </rules>
        </rewrite>
    </system.webServer>
    </configuration>
    

    我将这个精确的 sn-p 用于强制 HTTPS 重定向的网络服务器,但这似乎也与您所拥有的非常接近。你确定你已经正确配置了 web.config 文件的结构吗? - 我记得当我遗漏一些东西时遇到了问题。

    【讨论】:

    • 我不确定您所说的“web.config 文件的结构”是什么意思。
    • HTTP_HOST 值从何而来?也许它需要正确设置?
    • 在 Windows Web 服务器上,(如果重写规则是在站点级别定义的)那么您需要将此信息放在 web.config 文件中(就像我在上面编辑的那样),并将其放置在您的根目录中。 HTTP_HOST 是一个服务器变量,本质上是主机域:docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/…
    • 是的,我正在使用 web.config 文件。重定向正在重定向到主机。问题是我需要它重定向到主机的子域“ExitInterview”。
    • 尝试将 {R:1} 更改为 {R:0}(这可能是您的问题):stackoverflow.com/questions/16998832/…
    猜你喜欢
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 2019-07-02
    • 2014-11-14
    • 2012-10-02
    相关资源
    最近更新 更多