views:

481

answers:

4

How could I go about mapping a domain alias, e.g. domainAlias.co.za, to a virtual folder under, e.g. mainDomain.co.za, so that all requests to domainAlias.co.za actually get served by mainDomain.co.za/domainAlias ?

A: 

You can use routing.

System.Web.Routing
Ropstah
A: 

I haven't used it, but IIS has a URL Rewrite Module that can import Apache mod_rewrite rules. There is also a document that compares IIS URL Rewriting and ASP.NET routing. With some research, you should be able to get that working.

Kevin Hakanson
@Kevin: Thanks, I'll check those out.
ProfK
The URL Rewrite Module works for IIS7, not on earlier versions of IIS.
Cheeso
+1  A: 

A URL Rewriter like IIRF lets you do this.

The rules would be:

RewriteCond %{HTTP_HOST}  ^(?!mainDomain)([^\.]+)\.co\.za$
RewriteRule ^/(.*)$       /%1/$1   [L]

In English, this rule says: if the host is NOT maindomain.co.za, but still ends in .co.za, then rewrite the URL so that it is prepended with /domainAlias/. With this rule, you get:

input                         output
-----                         ------
http://foo.co.za/a.php        http://main.co.za/foo/a.php
http://foo.co.za/a.aspx?r=1   http://main.co.za/foo/a.aspx?r=1

You can also go one level further and make the rewrite conditional on the presence of the directory, something like this:

RewriteCond %{HTTP_HOST}   ^(?!mainDomain)([^\.]+)\.co\.za$
RewriteCond c:\wwwroot\%1  -d
RewriteRule ^/(.*)$        /%1/$1   [L]

This says: if the host is not maindomain.co.za, AND the directory c:\wwwroot\domainAlias exists, then rewrite to prepend ....

But in that case you might instead want to do the converse - test for lack of existence of the directory - and redirect to a 404:

RewriteCond %{HTTP_HOST}   ^(?!mainDomain)([^\.]+)\.co\.za$
RewriteCond c:\wwwroot\%1  !-d
RewriteRule ^/(.*)$        -   [NF]

NF = 404

you can also do [F] which is a 503 (Forbidden).

IIRF works on IIS5, IIS6, or IIS7.

Cheeso
A: 

Thanks for great rewrite rule. But how the rule will be if the address may or may not include www e.g www.domain.com www.domain1.com domain1.com etc

Hamayun