Surendra Sharma

Surendra Sharma

Search This Blog

Saturday, September 12, 2015

How to implement 301 redirect with IIS URL rewrite module in ASP.NET



If Its common scenario that, if you created items first and later on creating workflow then you have to manually apply those workflow on template’s standard values and content items.

If your client website is already running and you have developed it again with same or different technology.

Once the coding and testing of this new website is complete. What next?

Deploy on server. Make it LIVE.

So there may be some old URL or pages which should be redirected to new website pages.
Good example is if somebody bookmarked the old URL then instead of page not found it should be redirected to new pages.

In simple terms it’s called 301 redirect.

How to achieve 301 redirect without writing code in ASP.NET?

You must have URL Rewrite module in IIS. If you have not then download and install from http://www.iis.net/downloads/microsoft/url-rewrite

Now make entries in web.config between <system.webServer> … </system.webServer> tag by specifying old URL pages mapping with new pages as below.

Suppose you old page URL is http://www.example.com/old.php and you want to redirect it to new page as http://www.example.com/new.aspx. Finally your config file entry looks like



<rewrite>
  <rules>

      <rule name="Rewrite Rule1" stopProcessing="true">
          <match url="world/new-zealand.php" />
          <action type="Redirect" url="world/east/country1.aspx" />
      </rule>
      <rule name="Rewrite Rule2" stopProcessing="true">
          <match url="old.php" />
          <action type="Redirect" url="new.aspx" />
      </rule>
      <rule name="Rewrite Rule3" stopProcessing="true">
          <match url="oldfile.pdf" />
          <action type="Redirect" url="newfile.pdf" />
      </rule>
      <rule name="Rewrite Rule4" stopProcessing="true">
          <match url="policy.pdf" />
          <action type="Redirect" url="/" />
      </rule>
      <rule name="Rewrite Rule5" stopProcessing="true">
          <match url="world/bahamas.php" />
          <action type="Redirect" url="bahamasnew.aspx" />
      </rule>
  </rules>
</rewrite>  

Here we are redirecting from request for any php page, pdf file to index page, new aspx pages.
You can specify any numbers of pages here. But this is good for limited pages. My suggestion is that its limit should be upto 50 pages.

If you have more than 50 pages then write the custom redirecting code.

You can do all these activities from IIS as well and IIS will make entry in web.config for you.

Please leave your comments or share this tip if it’s useful for you.

No comments:

Post a Comment