asp.net mvc 4 - IIS 7.5 URL重写:从旧域到新域的重定向规则似乎不起作用



我试图理解为什么当一个人试图进入网站时,在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响应,仅此而已。这让我觉得重写规则实际上被忽略了,但我不知道为什么。

{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> 

我为此挣扎了好几天。10-20重写规则我尝试过,失败的原因是:

  1. 如果您尝试在VisualStudio(2012/2013/2015)中重定向,它无法在实际的IIS托管站点中工作,因为VS在调试时生成自己的证书(当您在项目属性中指定时)以及权限问题由VS.处理
  2. IIS中的站点应该具有有效的证书(没有从启用了该/verisign的网站复制粘贴文件,甚至没有由snk.exe生成的自签名);请不要认为没有有效的证书你可以。(IIS 8和10中的自签名(也称为dev cert)对我有用;购买和自签名的区别在这里https://www.sslshopper.com/article-how-to-create-a-self-signed-certificate-in-iis-7.html)。应该安装证书,因为IIS可以有多个证书,但每个网站应该使用自己单独的证书。
  3. 站点绑定应该同时包含http(80)和https(443)
  4. 现在重定向语法出现在图片中;有几个在网上;你可以很容易地得到正确的正则表达式
  5. 故事的另一方面也必须考虑重定向可以使用全局处理。asax->Application_BeginRequest或ActionFilter在MVC 4/5。使用config或编程方式进行重定向可能导致不同的错误(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

UrlRewrite:

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

最新更新