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.

clip_image002

clip_image004

clip_image006

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:

clip_image007

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.

clip_image009

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

clip_image011

clip_image013

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" />

</conditions>

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

</rule>

 

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.

clip_image015

clip_image017

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" />

<conditions>

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

</conditions>

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

</rule>

 

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.

clip_image019

clip_image021

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

clip_image023

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}" />

</rule>

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

Hope this helps Smile

Comments (10)

  1. omar says:

    Hello,

    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

    thanks

    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
      https://support.microsoft.com/en-in/kb/2818415

      Regards,
      Chiranth

  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