Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
5 Jun 2010 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 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here