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>

Hi,
This is great info! I just wanted to point out a little typo in the script:
Should be:
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
You would create the website then change the root path by clicking on each website.
hi,
this is not working???
My hosting company have windows platform, I think they IIS6.
I get run time error with this code..
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.
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?
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
how can i write a rule to internally redirect from rocalabs.com/terms to rocalabs.com/terms.aspx ? We already have ur hosting account
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.
really very nice post… its working fine for me..
Thanks to Shai Ben-Naphtali.
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.
Hi, it is http://www.arvixe.com/contactus.php
What’s up to all, the contents present at this website are actually amazing for people experience, well, keep up the nice work fellows.