简体   繁体   中英

IIS 7.5 URL Rewrite: Redirect Rule does not appear to work from old domain to new domain

I am trying to understand why the following rule created in IIS is not working when a person tries to enter the site.

Basically we have an old domain and a new domain. I would like anyone going to the old domain to be redirected to a landing page on our new domain.

I am using ASP MVC4 for the site and I have added bindings for the domains and updated DNS as well.

My rule is:

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

At the moment if someone enters the old domain address the redirect does not do anything, the website just loads as if you were entering via the new domain to the home page.

Can someone tell me where I am going wrong here?

UPDATE The rule provided below still does not seem to work so I decided to just try and open my olddomain address in fiddler to see if I could see the redirect or response. All I get is a 200 HTTP response and nothing more. This makes me think that the rewrite rules are actually being ignored but I have no idea why.

{HTTP_HOST} will always just be the host name, and not include the protocol or path. Try changing your rule to this:

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

I had struggled on this for several days. 10-20 rewrite rule I tried and reasons of failure are:

  1. If you trying redirecting in VisualStudio(2012/2013/2015) it could not work in actual IIS hosted site as VS generates its own cert while debugging(when you specify in project properties) as well as permission issues are taken care by VS.
  2. The site in IIS should have valid(no copy paste of file from thawte/verisign enabled website or even self signed generated by snk.exe) certificate; please dont assume that without valid cert you can. (self signed(also known as dev cert) in IIS 8 and 10 worked for me; difference between purchased and self signed is here https://www.sslshopper.com/article-how-to-create-a-self-signed-certificate-in-iis-7.html ). The certificate should be installed as IIS can have multiple certs but each web site should use its own separate cert.
  3. The site bindings should have both http(80) and https(443)
  4. Now redirection syntax comes in picture; several are out there on Internet; you can get the correct regular expression easily
  5. Another side of story also has to be considered that redirecting can be handle using Global.asax->Application_BeginRequest or ActionFilter in MVC 4/5. Doing redirection using config or programmatically can lead to different errors(TOO_MANY_REDIRECTS, in web.config)
  6. Another gotcha I faced is redirecting from http->https is working fine but I am not able to comeback from https->http;
  7. Consider your scenario(and generally should not mix) out of the available choices

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM