URL Rewrite- Part 2(Inbound Rules)

Why URL Rewrite?

  1. Define powerful rules to transform complex URLs into simple and consistent Web addresses

http://Chiranth.com/get/ppt.aspx?author=Chiranth&topic=urlrewrite vs http://Chiranth.com/get/ppt/author/Chiranth/urlrewrite

      2. Preventing hot-linking

      3. Search engine friendly URLs

Enforce http://www.mysite.com instead of http://mysite.com

Why do we need inbound rules?

If we need to modify the incoming request uri or request headers before the request is handed over to the execution engine in IIS we can make use of URL Rewrite inbound rules.

How to create an inbound rule:

Over here we will be discussing the use of URL rewrite inbound rules independent of ARR. To create an inbound rule, click on the website->go to urlrewrite->add rule(s)->Blank rule under Inbound rules.




Important Sections in an Inbound rule:

• Pattern

• Conditions

• Server Variables

• Action Type- Rewrite, Redirect, abort, Custom Response, None

Mapping parts of an URL to sections in Inbound rule:


Creating sample URL Rewrite Inbound rules:

Scenario 1:

We want to redirect all the requests coming in over http to https.

For eg: I want to redirect requests coming in as http://contoso.com to https://contoso.com . also even if the request comes in as http://contoso.com/myapp/a.aspx this should also be redirected to https://contoso.com/myapp/a.aspx

Over here the pattern will be .* as we want to match and store the value of the uri present in the URL after the hostname. That is over here in our example .* matches with myapp/a.aspx.

Also note that pattern section expressions after match will be stored in {R:x} variable series where x is integer.


Also we want to make sure that this rule executes only when the request comes in over http or else we might end up in an infinite loop.

So add a condition stating that {HTTPS} is OFF.

And Action type will be redirect.

Action URL will be https://contoso.com/{R:0}

Below is how the rule will look like in the UI



Note: {R:0} will contain the whole pattern matched. You can also store sub-strings in different {R:x} variable series by specifying the specific patterns within braces. For example input string is myapp/a.aspx and pattern is myapp/(.*) then {R:0} will contain the value myapp/a.aspx and {R:1} will contain a.aspx.

Below is how the rule will look like in the web.config of your site.

<rule name="httpsredirect" enabled="false" patternSyntax="ECMAScript" stopProcessing="true">

<match url=".*" />

<conditions logicalGrouping="MatchAll" trackAllCaptures="false">

<add input="{HTTPS}" pattern="OFF" />


<action type="Redirect" url="https://contoso.com/{R:0}" />



Scenario 2:

Previous example talks about a url rewrite rule redirecting the request. Let’s consider a scenario where we can rewrite the requests.

Consider a scenario where we have 2 pages a.aspx and b.aspx under the site contoso.com and we want the requests coming in for a.aspx should be rewritten to b.aspx (http://contosso.com/a.aspx->http://contosso.com/b.aspx). Note that only server knows over here that the resource has been changed to b.aspx and the client doesn’t know about it so the url won’t change and it will remain as http://contosso.com/a.aspx in IE.

Note: when you are rewriting the requests using urlrewrite without ARR we need to keep in mind that we can’t change the protocol of the request or the hostname part of it. We can only change the request headers and the request uri or pattern section.

For eg: in http://contoso.com/app_path/….., only highlighted part can be modified.

Solution: to achieve the above requirement, create a rewrite inbound rule at the site level.

Over here we need to have a pattern which matches with a.aspx so the pattern section starts and ends with a.aspx. So pattern would be ^a.aspx$.

Note: go through the previous blog for info on regular expression http://blogs.msdn.com/b/chiranth/archive/2014/06/10/url-rewrite-part-1-prerequisites.aspx

If you want you can add a condition stating the this rule can be only executed when the hostname in the URL is contoso.com by adding a condition for {HTTP_HOST} matching contoso.com

Action type would be Rewrite

Rewrite URL would be b.aspx

In rewrite url we can only modify the URI part of the url as I had previously mentioned when we use rewrite without ARR.

Below is how the rule looks like in IIS UI.



Below is how the rule will look like in the web.config of your site.

<rule name="normalrewrite" enabled="false" patternSyntax="ECMAScript">

<match url="^a.aspx$" negate="false" />


<add input="{HTTP_HOST}" pattern="contoso.com" />


<action type="Rewrite" url="b.aspx" />



Scenario 3:

Imagine you had a site contoso.com and an application called oldsiteroot under the site, you have migrated the application from old server to new server and instead of placing the content by creating the application oldsiteroot, you have placed the content directly under the site. The users who have no idea will still be accessing the site as http://contoso.com/oldsiteroot/(contentpath) but now in the new server content location is http://contoso.com/(contentpath)

So the requests coming in as http://contoso.com/oldsiteroot/(contentpath) should be rewritten to http://contoso.com/(contentpath)

So over here pattern should match to the URI containing oldsiteroot and the resource location, also we need to exclude the ‘oldsiteroot’ and store resource name in a variable so that we can use that to append to the new rewritten url.

I had previously mentioned that you can store any number of values or patterns in {R:x} variable by enclosing the required pattern section within the braces ().

The pattern over here would be ^oldsiteroot/(.*). If the url is http://contoso.com/oldsiteroot/a.aspx then {R:0} will contain the whole pattern that is oldsiteroot/a.aspx and {R:1} will contain a.aspx

Consider the below two scenarios.



Action Type would be Rewrite and rewrite url would be {R:1} which would include the URI part excluding oldsiteroot.

Below is how the rule looks like in IIS UI


Below is how the rule looks like in web.config of your site.

<rule name="customrequirement" enabled="false">

<match url="^oldsiteroot/(.*)" />

<action type="Rewrite" url="{R:1}" />


In the next blog we will discuss about outbound rules and rewrite maps.

Hope this helps Smile

Comments (18)

  1. omar says:


    We are hosting two different web application in our company. Some of our web sites are hosted on

    IIS7 over Windows 2012. And the others are hosted on Apache over Windows 2012. We are keeping

    these sites on virtual servers that uses Shared Configurations. And all of them is bound to ARR 3.0.

    We are using different Web Farms for our Apache and IIS web sites.

    I need to implement a URL ReWrite Rule for the structure mentioned above. Is it recommended to use different web farm structures for this situation.

    Thank you for your assistance in advance.

  2. @Omar : I see you have mentioned that we are already using ARR. Now you want to create an ARR server with inbound rules to load balance the traffic? Is my understanding correct? Or could you please elaborate more.

  3. Thiago Beier says:

    guys what is the best way to use this redirect (that’s working here for me) and add on it /owa for those who hit http://webmail.domain/ instead of the full url https://webmail.domain/owa/ ? regards,

    1. @Thiago: i didnt get your questio. SO as per my understanding you want to redirect users coming with url http://webmail.domain/ to https://webmail.domain/owa. Please correct me if i am wrong. if above is the requirement then in the pattern section you can mention ^$ which means there is nothing in the request uri and redirect will be https://webmail.domain/owa

  4. Simon says:

    Hi all,

    Is there a way of changing to https and changing the hostname whilst passing the query string through ? e.g. http://oldsite/news.cfm?id=100 —> https://newsite/news.cfm?id=100


    1. Hi Simon,

      By default the query string will be appended if you have the append query string check box checked in the url rewrite rule. A sample rule can look like below

      pattern will be .*
      There will be 2 conditions
      {HTTPS} is OFF
      {HTTP_HOST} is oldsite

      Action Type will be redirect
      Action Url will https://newsite/{R:0}
      And also make sure append query string below is checked

      Hope this answered your query

  5. Is URL Rewrite an option for creating a friendly URL to a SharePoint 2013 site that is configured for Kerberos authentication?

    Desired: intranet
    Existing: https://portal.domain.,com:446

    1. Hi Michael,

      There are some limitations for url rewrite along with Sharepoint. Url rewrite along with Sharepoint works fine if you want to achieve a redirect. But when it comes to rewriting the url’s there might be some challenges which is explained in the below kb article


  6. khoaitspaz says:

    Hello Chiranth Ramaswamy,
    Thanks for your article 🙂 I want when clients type “http://www.abc.com”, it will be redirected (NOT rewrite) to “http://www.123.com”. Please show me how to do it with URLRewrite

    Many thanks,

    1. @khoaitspaz: For this you can create an inbound rule with pattern as .* that is matching everything and in conditions section add a condition for {HTTP_HOST} matching http://www.abc.com and action type as redirect. Action url will be http://www.123.com/{R:0} R:0 will contain if there w is anything present in the request uri

  7. Mon says:


    My IIS have an existing http redirect, meaning the url http://abc.com -> http://def.com. Right now have a request to redirect the specific page for example. http://abc.com/page.aspx -> http://def.com/page.aspx. What i need is both scenario will work. I’m planning to remove the http redirect and make url writer. how can we setup a rules for both scenarios?

    Thank you.

    1. Consider redirect from http://abc.com/page.aspx to http://def.com/page.aspx i.e for a specific page. The pattern in the inbound rule will be ^page.aspx$ and then the action type will be redirect and the action url will be http://def.com/page.aspx or http://def.com/{R:0} . If you want it to be only for abc.com you can add a condition that {HTTP_HOST} should match abc.com
      For a general case everything will be the same except pattern will be .* i.e match everything

  8. Greg says:

    how can i rewrite my url like that?
    Original URL: /article.asp?id=234&title=some-title
    Final URL: /article/234/some-title

    since i have parametes i don’t know hot to set up my pattern

    1. @Greg: Let me know if i have understood the problem statement correctly. The incoming url will be like /article.asp?id=234&title=some-title and you want to rewrite this to /article/234/some-title. for this the pattern will be (.*)(\.asp). when you have a pattern like the one mentioned {R:1} will contain article and {R:2} will contain .asp.
      Next you will have to fetch the values in the Query String. For this in the conditions add a new condition as {QUERY_String} matches .*id=(.*)&title=(.*) . the pattern might need some tweek as i havent tested it. But the crux you should be able to store 234 in {C:1} and some-title in{C:2} variables. Now in the action url part the action url will be {R:1}/{C:1}/{C:2} and make sure you uncheck append query string option below the action url part. Hope this helps.

  9. Tomer says:

    Hi, is it possible to define the rewrite map with full url path? for example:

    How? Thanks!

    1. @Tomer: Information and example of rewrite map is available in the next series of this article.
      Instead of just the page name in the example you can test it out for the complete URI. for example http://host.com/a/a.aspx. Here you can test out for the URI a/a.aspx in the key section and replace with the value you like. More examples on rewrite map is available in the iis.net article

      Hope this helps

  10. Fabiano Iacusso says:


    Having real difficulty accomplishing the following…

    User types in mywebspace.domain.edu, redirect to myq.domain.edu/MyWebspace.

    Now, if a user types in mywebspace.domain.edu/sitename, do not redirect to myq.domain.edu/MyWebSpace and have them land on mywebspace.domain.edu/sitename.

    Is this possible? So basically, only redirect mywebspace.domain.edu to a new URL, and let all other paths process on the original webserver.

    Thank you,

    1. Hi Fabiano,
      The above is possible, i.e in the pattern check for ^$ which means that there is nothing in the url after hostanme(match empty) and modify action as myq.domain.edu/MyWebSpace and action type as redirect. you can also add in a condition stating {HTTP_HOST} matches mywebspace.domain.edu so that it doesnt affect other hostnames if present.
      The above will only kick in for http://mywebspace.domain.edu and not for mywebspace.domain.edu/sitename.

      Hope this helps.


Skip to main content