Scott Forsyth's Blog

Postings on IIS, ASP.NET, SQL Server, Webfarms and general system admin.

Cloud Resources

IIS Resources

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=".*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^(?!www)(.*)\.domain\.com$" />
    </conditions>
    <action type="Redirect" url="http://domain.com/{C:1}/{R:0}" />
</rule> 

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=".*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^(?!www)(.*)\.domain\.com$" />
    </conditions>
    <action type="Rewrite" url="/{C:1}/{R:0}" />
</rule>   

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.

Posted: Nov 23 2011, 02:09 PM by OWScott | with 18 comment(s)
Filed under: , ,

Comments

cptJack said:

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?

# March 19, 2012 7:18 PM

OWScott said:

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.

<rule name="3rd level to 2nd level redirect" stopProcessing="true">

   <match url=".*" />

   <conditions>

       <add input="{HTTP_HOST}" pattern="^(.+)\.sample\.com$" />

   </conditions>

   <action type="Redirect" url="http://{C:1}.com{URL}" />

</rule>

# July 25, 2012 6:33 PM

OWScott said:

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.

# August 1, 2012 10:13 AM

Oscar Martin said:

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?

# August 10, 2012 5:51 PM

OWScott said:

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: dotnetslackers.com/.../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.

# August 10, 2012 7:14 PM

Oscar Martin said:

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

Regards.

# August 10, 2012 8:35 PM

Oscar Martin said:

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

Regards.

# August 10, 2012 9:06 PM

OWScott said:

Hi Oscar,

The 502 should be either a timeout or because the health test is failing.  Here's some more on that: weblogs.asp.net/.../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.

# August 11, 2012 11:31 PM

OWScott said:

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:

<rule name="CName to URL" stopProcessing="true">

   <match url=".*" />

   <conditions>

       <add input="{HTTP_HOST}" pattern="^(?!www)(.*)\.domain\.com$" />

       <add input="{URL}" pattern="^/user" />

   </conditions>

   <action type="Redirect" url="http://domain.com" />

</rule>

# September 28, 2012 4:54 PM

My2ndLovE said:

Hi,

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

?

Thanks

# December 21, 2012 4:57 AM

OWScott said:

@My2ndLovE,

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

<rule name="Redirect to a new domain" stopProcessing="true">

   <match url=".*" />

   <conditions>

       <add input="{HTTP_HOST}" pattern="^subdomain\.olddomain\.com$" />

   </conditions>

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

</rule>

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.

# December 23, 2012 5:07 PM

My2ndLovE said:

Hi,

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

<?xml version="1.0"?>

<configuration>

<system.webServer>

<rewrite>

<globalRules>

<rule name="Redirect to a new domain" stopProcessing="true">

  <match url=".*" />

  <conditions>

      <add input="{HTTP_HOST}" pattern="^subdomain\.olddomain\.com$" />

  </conditions>

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

</rule>

</rewrite>

</system.webServer>

<system.web>

<compilation debug="false" targetFramework="4.0" />

</system.web>

</configuration>

# December 25, 2012 4:53 PM

OWScott said:

@My2ndLovE,

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

<system.webServer>

<rewrite>

<rules>

<rule name="Redirect to a new domain" stopProcessing="true">

 <match url=".*" />

 <conditions>

     <add input="{HTTP_HOST}" pattern="^subdomain\.olddomain\.com$" />

 </conditions>

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

</rule>

</rules>

</rewrite>

</system.webServer>

# December 28, 2012 11:12 PM

OWScott said:

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.

# May 31, 2013 9:53 AM

Rick said:

How about rewriting (not redirecting)

deptone.domain.com/folderone

to

subdomain.domain.com/.../folderone

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.

# July 19, 2013 5:58 PM

OWScott said:

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://...).

# July 19, 2013 6:06 PM

Rodrigo said:

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

Thanks!

Rodrigo

# November 29, 2013 7:47 AM

OWScott said:

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:

<rule name="Domain redirect">

   <match url=".*" />

   <conditions>

       <add input="{HTTP_HOST}" pattern="^(www\.)?domain\.com$" />

   </conditions>

   <action type="Rewrite" url="http://domain.com.br/{R:0}" />

</rule>

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

# January 13, 2014 10:55 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)