How to do a rewrite rule in web.config

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>

Tags: , , , | Posted under DotNet/Windows Hosting, Programming/Coding | RSS 2.0

15 Comments on How to do a rewrite rule in web.config

  1. Chris says:

    Hi,

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

    Should be:

    redirectType="Permanent">

  2. 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. darko says:

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

  4. 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?

  5. 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

  6. Gaurav Rana says:

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

  7. karan says:

    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.

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

  9. 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.

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

  11. Thanks for the article, is

    there any way I can get an update sent in an email whenever
    you make a fresh post?

Leave a Reply

Your email address will not be published. Required fields are marked *


7 − = 3

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>