URL Rewrite city.domain.com to domain.com/city

An IIS 7.x URL Rewrite question that comes up often is how to redirect something.domain.com to domain.com/city.

Here’s an example URL Rewrite rule to accomplish that:

<rule name="CName to URL" stopProcessing="true">
    <match url=".*" />
        <add input="{HTTP_HOST}" pattern="^(?!www)(.*)\.domain\.com$" />
    <action type="Redirect" url="http://domain.com/{C:1}/{R:0}" />

This will redirect http://anything_except_www.domain.com to http://domain.com/anything_except_www.

It will also maintain the URL and querystring, so http://subdomain.domain.com/aboutus?more=info will redirect to http://domain.com/subdomain/aboutus?more=info.

It’s also possible to do this with a rewrite rule instead so that the original URL is maintained while the server sees the rewritten path.  That rewrite rule will look like this:

<rule name="CName to URL - Rewrite" stopProcessing="true">
    <match url=".*" />
        <add input="{HTTP_HOST}" pattern="^(?!www)(.*)\.domain\.com$" />
    <action type="Rewrite" url="/{C:1}/{R:0}" />

Basically this rule will look like http://subdomain.domain.com/aboutus/ to the site visitor but it will really hit siteroot/subdomain/aboutus/ on the server.

Note that there are other considerations for rewrites which are covered in more depth here.


  • Hey there. Have the same task as Ian - to switch sub.domain.com/path/exe?someparam=1 to sub.domain.com/path/exe?someparam=1&subdom=sub

    Is there some ready-made solution described in details?

  • Hi Anuj,

    In that case you would need to own edu.samples.com and edu.com. We have to assume something for the TLD so I assumed .com.

  • Hi Blisser,

    What are you trying to achieve with this rule? I see a couple things which may need to change.

    That rule doesn't utilize the subdomain in the redirect. Basically it's dropping the www and if there's a 3rd level domain before the pumba.name it will drop that too.

    The redirect should start with http:// for the redirect to work correctly. And the XML seems like there's an extra piece in it after the action element.

    If you let me know more specifically what you're doing I'll see if I can put together an example for you.

  • Hi, Im really new un this and im looking for redirect a subfolder to another application in another server farm.

    I've an ARR load balancer, witch contains two server farms (farm1,farm2) in the load balancer i have the application test-lb.domain.com.mx, this traffic is sended to the farm1, but if the client request for test-lb.pricetravel.com.mx/blog i want to send it to the farm2.

    is this is possible?

  • Hi Oscar,

    Yes, ARR fully supports that. You can create two URL Rewrite rules, one with the URL match value of "^blog(.*)" which will get everything that starts with blog, and set it to not process any further rules. The second rule should have ".*" instead so that it catches everything else. Each rule would point to the server farm you specify.

    My recommendation is to spend 15 minutes and watch this video: http://dotnetslackers.com/articles/iis/Introduction-to-Application-Request-Routing-Week-31.aspx. I give an intro to ARR there and cover some of the rules. There are a number of other videos which cover this in more depth. Hopefully this points you in the right direction. Feel free to post back here with more questions.

  • Hi Scott, thank u very much for ur quick response and support, i will be watching the videos.


  • Hi Scott, i was watching the videos, then i try to write a rule to send some request to a server farm with the server www.google.com but i have an error:

    502 - web server received an invalid response while acting as a gateway or proxy server.

    Have u see this error before? i was reading about the problem and i saw that it could be a problem with the process called w3wp.exe and need modify the windows registry, so im not sure if thats the solution.

    i wrote the rules that u told me before and work very well with .* to sent it to my farm1, but the error ocurr with the second rule "^blog(/.*)".

    Also, in reallity, my problem is that im trying to send test-lb.domain.com.mx/blog to another server farm because in that farm i want to mount a wordpress or wiki, but i need that the user still thinking he is in the test-lb.domain.com.mx/blog


  • Hi Oscar,

    The 502 should be either a timeout or because the health test is failing. Here's some more on that: http://weblogs.asp.net/owscott/archive/2010/10/26/what-the-failed-requests-counter-in-arr-really-means.aspx

    I think that you're on the right track and it may be as easy as the health checking failing.

    A good way to test is to send your /blogs rule to the other web farm and see if it works. That will let you know if the issue is the rule, or if the issue is the server farm.

    Hopefully this points you in the right direction. Just a heads up that I'll be away from my computer for a week so if you have any questions I'll be slow to respond for one week.

  • Hi Soma,

    So the 'user' and the 'city' will be dropped completely, and not reflected anywhere else in the URL? In other words, newyork.domain.com and seattle.domain.com will respond the same. Is that ok? Basically it will go directly to domain.com and just retain the querystring as long as the pattern is optiona-www.something.domain.com and the URL starts with user.

    If so, here's what the rule will look like:

  • Hi,

    How about redirect from http://subdomain.olddomain.com/page1/topic1 to http://newdomain.com/page1/topic1


  • @My2ndLovE,

    If it's just one domain to another while preserving the URL and querystring, you can do it like so:

    If you have a number of them then you can consider a rewrite map, and if you have a pattern in the domain name that you have other creative options too, but if I understand your request, then this should do the trick.

  • Hi,

    This is my web.config file, anything wrong? I received 500 internal error.

  • @My2ndLovE,

    Since this is for your web.config, use rather than It's also missing the closing tag. So it should look like this:

  • Hi abhi shah,

    Yes, there are two ways you can achieve that. One is to perform a redirect. That means that the URL will change in the browser, but it's the most straight forward and you don't need to worry about relative links or anything.

    The other is to use the reverse proxy functionality. It will 'tunnel' the traffic through www.site.com to the logs.site.com domain. To get that functionality you must first install ARR, then at the site level create a new rule. There is a reverse proxy option which should lead you through the process of creating the rule.

  • How about rewriting (not redirecting)




    Plus the source URL would need to support deptone.domain.com and deptwo.domain.com , etc.

    DNS has subdomain.domain.com on a IIS site on server 2. DNS has deptone.domain.com to an IIS site on Server 1. Server 1 is in the DMZ, and Server 2 is in a protected DMZ. Both have ARR/URL Rewrite installed, and both have ARR Server Proxy Settings "Enable Proxy' checkbox'd.

    I can only get this to work with a 30x Redirect rule.

  • Hi Rick,

    As long as you have ARR installed then you can do it through either A) an ARR webfarm (site or server level) or B) reverse proxy rule at the site level.

    The reverse proxy rule is good for single server situations where you simply want a reverse proxy. It needs to be created at the site level though. Apart from that possibility it sounds like you have it set correctly.

    A good way to create the base rule is to use a add rule wizard in URL Rewrite. That should create the example rule, but it's simply a rewrite with the full protocol (http://...).

  • Hi,

    It is possible redirect an URL (domain) like this?

    www.domain.com to domain.com.br

    The idea is that if an user put the url www.domain.com, it always change to > domain.com.BR (with .BR at the end).


  • Hi Rodrigo,

    Sorry for the really long delay in replying. If you happen to see this still, the way to do that would be with a rule like this:

    That watches for www.domain.com or domain.com and redirects to domain.com.br, and it maintains the URL and querystring.

Comments have been disabled for this content.