views:

1824

answers:

2

How do I create subdomains in ASP.NET? I have few links on my homepage(Home.aspx) Example: Link1 and Link2

I need to create two sub domains link1.example.com and link2.example.com and redirect to them. I will be using Response.Redirect for navigation.

Questions: Do the files need to reside in different directory or something? Can I test or simulate this example on localhost?

Any help to this newbie ASP.NET programmer will be appreciated.

A: 

You need to set up domains and sub-domains from your Domain Provider (from where you bought your domain name), not from your ASP.NET website.

Locally, you cannot test this because you can only access http://localhost as the dev web server.

Andreas Grech
+4  A: 

Depending on what you are actually trying to accomplish with these redirects, this may be useful. If you are trying simply to have multiple sub-domains handled from a single web application, you can fake this sub-domain behavior outside of ASP.NET entirely using an IIS-based server hosting a single web application with an ISAPI rewrite tool such as Ionics Isapi Rewrite Filter (http://www.codeplex.com/IIRF), assuming you have the ability to add an ISAPI filter to your hosting environment.

Since the ISAPI filter is handled before ASP.NET even knows about the request, you can host a number of sub-domains (and full domains) from just one web application. Unfortunately, emulating this type of redirect in the ASP.NET built-in web server (Cassini) is not possible because it doesn't use ISAPI. This method does allow you to test the functionality of your final sub-domains, but only at the final redirect location; you can't really test the original domain map in Cassini either since the Windows hosts file doesn't allow rules with ports.

If you set up your single application to have pages to handle the functionality of all of your desired sub-domains, you can simply redirect the request for the sub-domain into your single application at the desired location. This can be as simple as a folder of pages within your single application that handles all the logic for your new sub-domain. Here are some example rewrite rules for Ionics that will send requests for the various sub-domains to the final location in your single web project:

RewriteCond %{HTTP_Host} ^(?~new-sub-domain.yourdomain.com).*$ [I]
RewriteRule ^(.*)$ http://www.yourdomain.com/new-sub-domain$1 [I,R=302]
RewriteCond %{HTTP_Host} ^(?~another-new-sub-domain.yourdomain.com).*$ [I]
RewriteRule ^(.*)$ http://www.yourdomain.com/another-new-sub-domain$1 [I,R=302]
# '#' is a comment marker in the rewrite files
# [I] at the end means case-insensitive
# [L] at the end  means last rule (sending the request on but hiding the forward from the end user)
# [R] at the end  means an official redirect and can be used instead of [L] (the end user will then see a new request, such as a 301 redirect using [R=301])

This will cause all requests to the new sub-domains to go to a directory within your existing project (/new-sub-domain/ and /another-new-sub-domain/, respectively); this can be modified to send them wherever you want within that project.

While this approach allows you to host several sub-domains (and potentially several full domains) from a single application, the restriction on redirect/rewrites within the ASP.NET web server (Cassini) will leave you restricted to testing the functionality of these final location (e.g., "/new-sub-domain/").

patridge