Friday, January 17, 2014

Godaddy IIS Hosting URL redirection using web.config for SEO

For SEO it is very common for redirecting url.
The scenarios are
Redirecting non-www site to www
Redirecting a popular link which is removed to a new link.

The redirection module in IIS 7 in godaddy hosting is already installed. But I couldn't get support for doing these.

Redirection or rewrite rule is done using web.config. which is placed in the root folder of the website.

Lets take an example of site nirays.com



I need to redirect
1) nirays.com to www.nirays.com
2) nirays.com/old1.php to www.nirays.com/new1.php
3) nirays.com/old2.php to www.nirays.com/newfolder/old2.php

I was not familiar with IIS for redirection, a Linux box with apache is my favorite.
I searched in the net and found an example to redirect from non-www to www site the below code.

<rewrite>
  <rules>
    <rule name="Redirect to www" stopProcessing="true">
      <match url="(.*)" />
      <conditions trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^domain.com$" />
      </conditions>
      <action type="Redirect"
        url="{MapProtocol:{HTTPS}}://www.domain.com/{R:1}" />
    </rule>
  </rules>
  <rewriteMaps>
    <rewriteMap name="MapProtocol">
      <add key="on" value="https" />
      <add key="off" value="http" />
    </rewriteMap>
  </rewriteMaps>
</rewrite>
This code shall be placed in web.config between the xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
    </system.webServer>
</configuration>
After making this change along with replacing domain.com to nirays.com We were experiencing error code 500.
I was searching for a log for details in the hosting and was not able to find any. So the trick is to add the details as part of website itself by adding below code to web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
          .......
          .......
         <asp scriptErrorSentToBrowser="true"/>
<httpErrors errorMode="Detailed"/>
    </system.webServer>
</configuration>

This gave a clue that trackAllCaptures is not available for this version, so removed and voila it worked.
<conditions trackAllCaptures="false"> will become <conditions>

To tackle the other URL redirection they way is to use rewriteMap.
So the final web.config will look like

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
<rewrite>
  <rules>
    <rule name="Redirect to www" stopProcessing="true">
      <match url="(.*)" />
      <conditions >
        <add input="{HTTP_HOST}" pattern="^nirays.com$" />
      </conditions>
      <action type="Redirect"
        url="{MapProtocol:{HTTPS}}://www.nirays.com/{R:1}" redirectType="Permanent"/>
    </rule>
  <rule name="Redirect Rule" stopProcessing="true">
        <match url=".*" />
        <conditions>
            <add input="{StaticRedirects:{REQUEST_URI}}" pattern="(.+)" />
        </conditions>
        <action type="Redirect" url="http://www.nirays.com/{C:1}" appendQueryString="False" redirectType="Permanent" />
    </rule>
  </rules>
  <rewriteMaps>
    <rewriteMap name="MapProtocol">
      <add key="on" value="https" />
      <add key="off" value="http" />
    </rewriteMap>
 <rewriteMap name="StaticRedirects">
        <add key="/old1.php" value="new1.php" />
        <add key="/old2.php" value="newfolder/old2.php" />
    </rewriteMap>
  </rewriteMaps>
</rewrite>
     <!--<asp scriptErrorSentToBrowser="true"/>
  <httpErrors errorMode="Detailed"/>  -->
    </system.webServer>
</configuration> 

Make sure that  <asp scriptErrorSentToBrowser="true"/>  <httpErrors errorMode="Detailed"/> is removed or commented out.

No comments:

Post a Comment

Contributors