【问题标题】:IIS 7.5 URL Rewrite: Redirect Rule does not appear to work from old domain to new domainIIS 7.5 URL 重写:重定向规则似乎不适用于从旧域到新域
【发布时间】:2015-08-16 10:20:25
【问题描述】:

我试图了解为什么当有人尝试进入该站点时,在 IIS 中创建的以下规则不起作用。

基本上我们有一个旧域和一个新域。我希望任何访问旧域的人都被重定向到我们新域的登录页面。

我正在为站点使用 ASP MVC4,并且我已经为域添加了绑定并更新了 DNS。

我的规则是:

               <rule name="http://www.olddomain.com to landing page" patternSyntax="Wildcard" stopProcessing="true">
                <match url="*" />
                <action type="Redirect" url="http://www.new-domain.co.uk/LandingPage" />
                <conditions logicalGrouping="MatchAny">
                    <add input="{HTTP_HOST}" pattern="http://www.olddomain.com" />
                    <add input="{HTTP_HOST}" pattern="http://olddomain.com" />
                    <add input="{HTTP_HOST}" pattern="http://www.olddomain.com/" />
                    <add input="{HTTP_HOST}" pattern="http://olddomain.com/" />
                </conditions>
            </rule> 

目前,如果有人输入旧域地址,重定向不会做任何事情,网站只会加载,就像您通过新域进入主页一样。

谁能告诉我这里哪里出错了?

更新 下面提供的规则似乎仍然不起作用,所以我决定尝试在提琴手中打开我的旧域地址,看看我是否能看到重定向或响应。我得到的只是一个 200 HTTP 响应,仅此而已。这让我觉得重写规则实际上被忽略了,但我不知道为什么。

【问题讨论】:

    标签: asp.net-mvc-4 url redirect iis url-rewrite-module


    【解决方案1】:

    我为此苦苦挣扎了好几天。 10-20 rewrite rule 我试过了,失败的原因是:

    1. 如果您尝试在 VisualStudio(2012/2013/2015) 中进行重定向,则它无法在实际的 IIS 托管站点中工作,因为 VS 在调试时生成自己的证书(当您在项目属性中指定时)以及权限问题由对比。
    2. IIS 中的站点应该有有效的(没有从 thawte/verisign 启用的网站复制粘贴文件,甚至是由 snk.exe 生成的自签名)证书;请不要假设没有有效的证书就可以。 (IIS 8 和 10 中的自签名(也称为开发证书)对我有用;购买和自签名之间的区别在这里 https://www.sslshopper.com/article-how-to-create-a-self-signed-certificate-in-iis-7.html)。应该安装证书,因为 IIS 可以有多个证书,但每个网站都应该使用自己单独的证书。
    3. 站点绑定应同时具有 http(80) 和 https(443)
    4. 现在重定向语法出现了;有几个在互联网上;您可以轻松获得正确的正则表达式
    5. 另一方面,还必须考虑可以使用 MVC 4/5 中的 Global.asax->Application_BeginRequest 或 ActionFilter 处理重定向。 使用配置或以编程方式进行重定向可能会导致不同的错误(TOO_MANY_REDIRECTS,在 web.config 中)
    6. 我面临的另一个问题是从 http->https 重定向工作正常,但我无法从 https->http 恢复;
    7. 从可用选项中考虑您的方案(通常不应混合使用)

    HttpRedirect:

    Request 1 (from client):    Get file.htm
    Response 1 (from server): The file is moved, please request the file newFileName.htm
    Request 2 (from client):    Get newFileName.htm
    Response 2 (from server): Here is the content of newFileName.htm
    

    网址重写:

    Request 1 (from client):     Get file.htm
    URL Rewriting (on server):   Translate the URL file.htm to file.asp
    Web application (on server): Process the request (run any code in file.asp)
    Response 1 (from server):    Here is the content of file.htm (note that the client does not know that this is the content of file.asp)
    whether you need HttpRedirect or UrlRewrite
    https://weblogs.asp.net/owscott/rewrite-vs-redirect-what-s-the-difference
    

    【讨论】:

      【解决方案2】:

      {HTTP_HOST} 始终只是主机名,不包括协议或路径。尝试将您的规则更改为:

      <rule name="http://www.olddomain.com to landing page" patternSyntax="Wildcard" stopProcessing="true">
          <match url="*" />
          <action type="Redirect" url="http://www.new-domain.co.uk/LandingPage" />
          <conditions logicalGrouping="MatchAny">
              <add input="{HTTP_HOST}" pattern="^www\.olddomain\.com$" />
              <add input="{HTTP_HOST}" pattern="^olddomain\.com$" />
          </conditions>
      </rule> 
      

      【讨论】:

      • 感谢您的回复。我刚刚注意到我用 HTTP_POST 打错了!所以感谢您发现这一点。然而,这个新规则仍然没有按预期工作。所发生的只是网站在其默认页面上打开,并且不会发生重定向
      • @DavidHirst 尝试将匹配 URL 更改为 &lt;match url=".+" /&gt;
      • 模式语法使用通配符。删除此属性并使用&lt;match url=".*" /&gt;
      • 这似乎仍然不适合我。请检查我上面的更新。
      猜你喜欢
      • 2011-08-07
      • 2013-08-14
      • 1970-01-01
      • 2015-10-07
      • 2014-04-06
      • 1970-01-01
      • 2015-05-11
      • 2015-07-09
      • 1970-01-01
      相关资源
      最近更新 更多