Click here to Skip to main content
15,886,362 members
Articles / Web Development / IIS
Tip/Trick

Sub Domain Implementation using Urlrewriter.NET (ASP.NET + IIS6)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
5 Jun 2010CPOL2 min read 18.4K   4   1
Urlrewriter.NET can implement sub-domains

Subject

You CAN implement URL rewriting for a virtual sub domain in your ASP.NET application using: Urlrewriter.NET + ASP.NET 2.0/3.5 under IIS6.

At first look, it's not obvious how this is done. However, using some code tips and snippets from around the web, I have accomplished this.

Background

My team had to upload an ASP.NET 3.5 application which actually held 2 applications. The first had to be available through www.strauss-group.com, and the second had to be available through OfficeCoffee.strauss-group.com.

After investigating some methods, including ISAPI filters, we decided to implement this feature using an HttpModule. We chose Urlrewriter.NET.

Everything was going quite smooth - until the point that we had to actually configure the rewriting in the web.config file. There seems to be lack of documentation around the internet. That said, I am writing this tip in light of the missing documentation.

Summary of Steps for Implementation

Step 1

Download the main package from Urlrewriter.NET.

Step 2

Follow the instructions in the readme.txt file (from the downloaded package). They tell you exactly what to add to the web.config file:

  1. Add the <configSection ...> directive
  2. Add the <httpModule ...> directive
  3. Add the <rewriter ...> directive

Step 3

Make sure the DNS servers point to your server (alternatively: see "testing" section below).

Step 4

Configure your <rewriter ...> section. Here is the relevant part of the web.config:

XML
<?xml version="1.0"?>
<configuration>
<rewriter>
    <if header="HTTP_HOST" match="^(officecoffee\.)([^.]+)\.([^.]+)$">
      <!--
		<rewrite url="~/afh/css/(.*)" to="~/afh/css/$1" processing="stop" ></rewrite>
		<rewrite url="~/afh/js/(.*)" to="~/afh/js/$1" processing="stop" ></rewrite>
		-->
      <rewrite url="~/Documents/(.*)" to="~/Documents/$1" processing="stop"></rewrite>
      <rewrite url="~/PageFiles/(.*)" to="~/PageFiles/$1" processing="stop"></rewrite>

      <rewrite url="~/he/(.*)" to="~/he/$1" processing="stop" ></rewrite>

      <rewrite url="~/afh(.*)" to="~/afh$1" processing="stop" ></rewrite>

      <rewrite url="~/ScriptResource.axd(.*)" to="~/ScriptResource.axd$1" processing="stop" >
      </rewrite>
      <rewrite url="~/WebResource.axd(.*)" to="~/WebResource.axd$1" processing="stop" >
      </rewrite>

      <rewrite url="/" to="/he/afh-folder/afhhomepage/" processing="stop" ></rewrite>

    </if>

  </rewriter>

Quick Explanation

The key to implementing the sub domain URL rewriting is the directive:

<if header="HTTP_HOST" match="^(officecoffee\.)([^.]+)\.([^.]+)$">

In fact - without that directive, you can't rewrite the URL on a sub-domain basis.

The "match" attribute is a RegEx which finds our sub domain pattern in the relevant part of the URL.

Another issue to notice is the processing="stop" attribute, which was very important for us. The instruction "stop" means that if a match was found and a URL rewriting was performed - than there is no need to further process the rewriting according to other <rewrite> sections. This was very useful for us since we were running the application under a CMS (specifically: EPiServer CMS). And CMS systems have their own "folders" and target URLs that come under consideration when applying URL rewriting.

Testing

You can configure your "hosts" file (c:\windows\system32\drivers\etc) and add the following lines:

127.0.0.1    www.strauss-group.com

127.0.0.1    OfficeCoffee.strauss-group.com 

This can simulate the DNS on your development machine.

Hope this helps my friends!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect ENGetica
Israel Israel
A Computers Engineer with accumulate 10 years of experience in varying software and business fields. Natural Entrepreneur. Technology and business enthusiast.

Comments and Discussions

 
GeneralHello Can you send sample code please. Pin
Member 306650528-Nov-10 5:41
Member 306650528-Nov-10 5:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.