Udostępnij za pośrednictwem


URL Rewrite- Part 2(Inbound Rules)

Why URL Rewrite?

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

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

      2. Preventing hot-linking

      3. Search engine friendly URLs

Enforce https://www.mysite.com instead of https://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 https://contoso.com to https://contoso.com . also even if the request comes in as https://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 (https://contosso.com/a.aspx->https://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 https://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 https://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 https://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 https://contoso.com/oldsiteroot/(contentpath) but now in the new server content location is https://contoso.com/(contentpath)

So the requests coming in as https://contoso.com/oldsiteroot/(contentpath) should be rewritten to https://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 https://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

Technorati Tags: IIS,URL rewrite,inbound rules,ARR,pattern,HTTP_HOST,HTTPS,redirect https

Comments

  • Anonymous
    November 09, 2014
    Hello,We are hosting two different web application in our company. Some of our web sites are hosted onIIS7 over Windows 2012. And the others are hosted on Apache over Windows 2012. We are keepingthese 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.
  • Anonymous
    November 13, 2014
    @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.
  • Anonymous
    April 18, 2016
    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,
    • Anonymous
      April 19, 2016
      @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
  • Anonymous
    June 13, 2016
    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=100thanks
    • Anonymous
      June 15, 2016
      The comment has been removed
      • Anonymous
        December 06, 2017
        Hi, I am trying to do almost the same thing actually,But the url in both cases are the same.Examle: http://domain/url.htm => https://domain/url.htmIt's a specific html page - that i want to redirect PERMANENT to https protocol.How is this possible? I am using IIS7 URL Rewrite.Thank You!
        • Anonymous
          December 06, 2017
          HI KarenYour issue is same as the one in scenario 1. You can follow the same steps as in the blog. .* in pattern means that it mathes anything after http://domain.com/{any_part here is mathed} . if you just want to match for url.htm mention url.htm in the pattern. if you want a permanent redirection and redirection to be cached on the client side you can change the redirect type to permanent in the dropdown. Hope this helpsRegards,Chiranth
  • Anonymous
    July 13, 2016
    Is URL Rewrite an option for creating a friendly URL to a SharePoint 2013 site that is configured for Kerberos authentication?Desired: intranetExisting: https://portal.domain.,com:446
    • Anonymous
      July 14, 2016
      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 articlehttps://support.microsoft.com/en-in/kb/2818415Regards,Chiranth
  • Anonymous
    July 27, 2016
    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 URLRewriteMany thanks,
    • Anonymous
      July 29, 2016
      @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 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
  • Anonymous
    October 24, 2016
    Hello,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.
    • Anonymous
      November 12, 2016
      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.comFor a general case everything will be the same except pattern will be .* i.e match everything
  • Anonymous
    November 15, 2016
    The comment has been removed
    • Anonymous
      November 15, 2016
      @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.
  • Anonymous
    January 16, 2017
    Hi, is it possible to define the rewrite map with full url path? for example: How? Thanks!
    • Anonymous
      January 17, 2017
      @Tomer: Information and example of rewrite map is available in the next series of this article.https://blogs.msdn.microsoft.com/chiranth/2014/06/13/url-rewrite-part-3outbound-rules-rewrite-maps/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 articlehttps://www.iis.net/learn/extensions/url-rewrite-module/using-rewrite-maps-in-url-rewrite-moduleHope this helps
  • Anonymous
    February 05, 2017
    Hello,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,
    • Anonymous
      February 20, 2017
      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.Regards,Chiranth
  • Anonymous
    April 18, 2017
    The comment has been removed
    • Anonymous
      May 07, 2017
      2 probable solutions i can think of is,1) you can create an alias or cname for the shortname to map to the actual hostname2) Create a A record for the Shortname.com to map to the web server or load balancer and create a dummy website with the hostname or add another hostname binding to the same exisitng binding. if you create another binding in the same site, nothing is required. if you create another site with the dummy binding, you need ARR along with url rewrite to rewrite the requests to the different site. create a rule at the new site level with pattern ./app. consition with HTTP_HOST matches shortname.com and action as http://myappservice.domain.com/{R:0}Hope this hels
  • Anonymous
    May 03, 2017
    Pretty! This was a really wonderful article. Many thanks for supplying this info.
  • Anonymous
    November 17, 2017
    Dear Cheran, I am new to url rewrite. When I simply wrote a inbound rule my css and image links broken. Can you suggest how to do other than tag and absolute url. Pl. reply.
    • Anonymous
      November 18, 2017
      HI, I think you might have written a general url rewrite rule which is affecting the images and css. What you can do is collected failed request tracing in IIS and see how the css and image url' are rewritten and also check what is the actual address of the url. One of the shortcuts is to exclude the rule to work for css or images by adding a condition stating request uri does not match pattern .*css
  • Anonymous
    November 17, 2017
    correction: other than usage of base tag and mentioning the absolute address in html.