How to do a rewrite rule in web.config

Written by Shai Ben-Naphtali Monday, 1 March 2010

If you would like to do a domain URL redirection from domain.com to www.domain.com or vise versa, you can do something like this:

Place either of these (depending on what you’d like done. And edit to match your domain) inside the <system.webServer></system.webServer> tags in the web.config of the domain.

<rewrite><rules>
<rule name=”Add WWW prefix” >
  <match url=”(.*)” ignoreCase=”true” />
    <conditions>
      <add input=”{HTTP_HOST}” pattern=”^domain\.com” />
    </conditions>
  <action type=”Redirect” url=”http://www.domain.com/{R:1}”
    redirectType=”Permanent” />
</rule>

<rule name=”Remove WWW prefix” >
  <match url=”(.*)” ignoreCase=”true” />
    <conditions>
      <add input=”{HTTP_HOST}” pattern=”^www\.domain\.com” />
    </conditions>
  <action type=”Redirect” url=”http://domain.com/{R:1}”
    redirectType=”Permanent” />
</rule>
</rules></rewrite>



13 Comments

  1. Chris   |  Tuesday, 23 March 2010 at 2:20 pm

    Hi,

    This is great info! I just wanted to point out a little typo in the script:

    Should be:

    redirectType="Permanent">

  2. Gary Bartlett   |  Sunday, 18 April 2010 at 10:07 am

    Hi,

    I would like to use this method to point different domains to different folders on my site, because I’m using HelpConsole as my website editing application. HelpConsole resides in /wwwroot and creates websites in subfolders. What would the code look like?

    If the domain is prodsol.com or http://www.prodsol.com, then go to the prodsol (/wwwroot/prodsol) folder;
    If the domain is http://www.systemicthinking.com, then go to the systemic (/wwwroot/systemic) folder;

    Thanks very much!

    Regards

    Gary

  3. Arvand Sabetian   |  Wednesday, 21 April 2010 at 8:40 pm

    You would create the website then change the root path by clicking on each website.

  4. darko   |  Monday, 07 June 2010 at 12:28 pm

    hi,
    this is not working???
    My hosting company have windows platform, I think they IIS6.
    I get run time error with this code..

  5. Arvand Sabetian   |  Thursday, 10 June 2010 at 7:06 am

    Unfortunately the instructions are for IIS 7. You may wish to try our hosting with the coupon code ‘mvchosting’ to try this out yourself on our platform.

  6. Christian Gnoth   |  Tuesday, 15 June 2010 at 12:28 am

    Hello,
    I tried to arrange an redirect for few files to the main index.php file on a godaddy windows iis7 webserver with the following rule:

    it is not working.

    The complete web.config files is:

    Can you please take a look what could be the reason for the error?

  7. Christian Gnoth   |  Tuesday, 15 June 2010 at 12:31 am

    the comment form do not takes my rules:

    it was :

    match url=”file-name.php”
    action type redirect url with http index.php redirectype permanent

  8. Gaurav Rana   |  Wednesday, 10 August 2011 at 5:18 am

    how can i write a rule to internally redirect from rocalabs.com/terms to rocalabs.com/terms.aspx ? We already have ur hosting account

  9. karan   |  Wednesday, 19 October 2011 at 12:23 pm

    would I be using the same rewrite rule above for pointing an old file say index-old.aspx to index-new.aspx to ensure that all SERP and traffic stays the same.

  10. Subhash Chand Sharma   |  Friday, 10 February 2012 at 7:24 am

    really very nice post… its working fine for me..
    Thanks to Shai Ben-Naphtali.

  11. Rules Of Success Online   |  Sunday, 01 July 2012 at 9:01 pm

    Does your site have a contact page? I’m having trouble locating it but, I’d like to send you an e-mail. I’ve got some suggestions for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it improve over time.

  12. Noah S   |  Monday, 02 July 2012 at 8:21 am

    Hi, it is http://www.arvixe.com/contactus.php

  13. Sierra Nevada Spain Forum Gandia   |  Wednesday, 10 October 2012 at 2:40 am

    What’s up to all, the contents present at this website are actually amazing for people experience, well, keep up the nice work fellows.

Leave a Reply






3 + six =