Tuesday, 5 June 2018

URL rewrite & redirect in .net web apps.

URL rewrite module. Example for default HTTP to HTTPS redirection. Corresponding XML must be placed in web.config as a child of <system.webServer>


    <rewrite>
      <rules>
        <clear />
        <rule name="Redirect to https" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" 
                redirectType="Permanent" appendQueryString="false" />
        </rule>
      </rules>
    </rewrite>

Http Redirect module. Example http://mysite/login to http://anothersite.com/login Corresponding XML must be placed in web.config as a first child of <configuration>.


  <location path="login">
    <system.webServer>
      <httpRedirect enabled="true" destination="http://anothersite.com/login"
                httpResponseStatus="Permanent" />
    </system.webServer>
  </location>

No comments:

Post a Comment