0
votes

I have redeveloped an old web forms website to ASP MVC web api. Some old urls appear on search engines and forums with the old web forms URL and query string, I need these to lookup by query string ID and redirect to new site with the variable name. I have added an ASP web form page to my root MVC application to perform the redirect. The problem is when I try the code on my localhost it works but then when I try it on the live website it redirects to the homepage. This could be because I have a rewrite rule to redirect all http: to https. I am not sure.

current bing url http://www.domain.com/View.aspx?ID=1

This works https://www.domain.com/View.aspx?ID=1 (https instead of http)

Redirects to http://www.domain.com/NewPath/SampleName

Code as follows

try
{
  string url = "https://domain.com/NewPath/" + getNewName(Request.QueryString["id"]);
  Response.Redirect(url, false);
  HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception)
{
  Response.Redirect("https://domain.com");
}

Web config rewrite rule

<location path="app/views">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlMode="DisableCache" />
      </staticContent>
    </system.webServer>
</location>

<rewrite>
      <rules>
        <rule name="SecureRedirect" stopProcessing="true">
          <match url="^(.*)$" />
          <conditions>
            <add input="{HTTPS}" pattern="off" />
            <add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
          </conditions>
          <action type="Redirect" url="https://{C:2}" redirectType="Permanent" />
        </rule>
      </rules>
</rewrite>
1

1 Answers

0
votes

IIS rewrite rule

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Redirect to Https" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="^OFF$" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

This URL helped http://www.jppinto.com/2010/03/automatically-redirect-http-requests-to-https-on-iis7-using-url-rewrite-2-0/