Scott Forsyth's Blog

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

.

  • Scott Forsyth

Hosting Needs

Training and Dev Labs

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

Ian said:

Fantastic! Thanks - I've been struggling with this for a while now.

I needed to switch sub.domain.com/path/exe?someparam=1

to sub.domain.com/path/exe?someparam=1&subdom=sub

and could NOT figure out how! Your example led me to the right path, really appreciate it!

# January 27, 2012 4:23 PM

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 cptJack,

The following rule should get you pretty close.  I haven't tested it a lot to make sure that it works in all situations.

A good way to test it is to make the 'rewrite' a 'redirect' so that you can see what it does.  When you're done testing then change it back to a 'rewrite' so that the user doesn't need to see the subdomain appended.

This will also catch www.domain.com so you can add another condition where it's not www.domain.com.  Just make sure that after you add conditions that you move the current condition to the end since the bottom condition is used for {C:1}.

And if this isn't close enough, if you explain in even more detail what you're looking for (like which domains will be redirected, and which won't be, etc) then I can target the rule even further.

<rule name="Add subdomain to querystring" stopProcessing="true">

   <match url=".*" />

   <conditions>

       <add input="{QUERY_STRING}" pattern="subdom=" negate="true" />

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

   </conditions>

   <action type="Rewrite" url="/{URL}?{QUERY_STRING}&amp;subdom={C:1}" appendQueryString="false" />

</rule>

# March 20, 2012 4:27 PM

Anuj said:

# July 25, 2012 4:33 AM

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

Blisser said:

Hello, Scott.

One question.

For rule

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

   <match url=".*" />

   <conditions>

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

   </conditions>

   <action type="Redirect" url="pumba.name/.../contacts.aspx"">pumba.name/.../contacts.aspx" />

  </rule>  

path  pumba.name/.../contacts.aspx works good, but in redirect gives error 403.14 i.e. goes to folder "inetpub".

How to get him to go to this address? =(

# August 1, 2012 10:03 AM

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

Soma Moitra said:

Hi Scott,

Thanks for the posting.

I need some help to convert from city.domain.com/user?id=123 to domain.com?id=123

Can you please help? Your example is converting to domain.com/city/user?id=123

# September 27, 2012 2:32 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
Leave a Comment

(required) 

(required) 

(optional)

(required)