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.

How to point different URL of same website to single domain name in ASP.NET



If you are implementing Google analytics, Google treats http://www.example.com and http://example.com as different website and give different analytics reports. But in original both sites are same.

Now your client demanding that both URL http://www.example.com and http://example.com should redirect to single URL as http://www.example.com

How to solve this?

You must have URL Rewrite module in IIS for this. 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 lists mapping with new single URL. Finally your config file entry looks like

<rewrite>
  <rules>
      <rule name="CanonicalHostNameRule1">
          <match url="(.*)" />
          <conditions>
              <add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
          </conditions>
          <action type="Redirect" url="http://www.example.com/{R:1}" />
      </rule>
  </rules>
</rewrite>  


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.