Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: Redirect using Web.config - IIS  (Read 4128 times)

0 Members and 1 Guest are viewing this topic.

sajugovind

  • Guest
Redirect using Web.config - IIS
« on: February 16, 2014, 04:46:55 pm »
For Linux one method for redirection is through .htaccess configuration. The same way we can configure redirection through Web.config for Windows. For this to work, you need to download and enable the URL Rewrite module for IIS 7. Then the following code is used in the ASP.NET web.config file:

Redirecting non-www to www:

Code: [Select]
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^domain.com$" />
          </conditions>
          <action type="Redirect" url="http://www.domain.com/{R:0}"
               redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>


web.config 301 redirect:

The first example will redirect single pages to a new location. For example, important pages of your site have .htm extensions and you want the new location to be its own directory (IE. http://domain.com/services.htm will change to http://domain.com/services/).

1. Open web.config in the directory where the old pages reside
2. Then add code for the old location path and new destination as follows:
Code: [Select]
<configuration>
  <location path="services.htm">
    <system.webServer>
      <httpRedirect enabled="true" destination="http://domain.com/services" httpResponseStatus="Permanent" />
    </system.webServer>
  </location>
  <location path="products.htm">
    <system.webServer>
      <httpRedirect enabled="true" destination="http://domain.com/products" httpResponseStatus="Permanent" />
    </system.webServer>
  </location>
</configuration>

You may add as many location paths as necessary.

The second example will redirect an entire directory to a new location. For example, if you want http://domain.com/olddir/ redirected to http://domain.com/newdir/ open web.config in /olddir and add the following line of code within the <system.webServer> section:
Code: [Select]
<httpRedirect enabled="true" destination="http://domain.com/newdir" httpResponseStatus="Permanent" />