Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I extract domain name from host header?

Status
Not open for further replies.

blove57

Programmer
Nov 27, 2007
6
0
0
US
I need to know the exact the domain name that the user is requesting. I am currently using Request.ServerVariables["HTTP_HOST"] but doesn't give eactly what I need. For example, if the HTTP_HOST is:

mydomain.com:8080
abc.mydomain.com
xyz.mydomain.com:5000

I need all these to return "mydomain.com". What's the best way to do this? I need to make sure the domain is only return since my application uses this info to differentiate different domain names pointing to the same web application. Any suggestions?
 
You are manually doing the work of a DNS server. You can also set this up in IIS or apache to read the abc.* to point to a different section of your site.

Doing this provides the IT team a bit more work rather than trying to maintain it yourself :)

 
I actually did something like this and it seemed to work for me so far.

I pass in any url, but usually request.Url.DnsSafeHost.

Then the method checks if what is passed in is an IP Address (via a method using a regex). If it is, it returns right then.

Next, it splits the url parameter based on periods. If the resulting array is less than 2 parts long (ie localhost) it just returns itself again.

If it is two parts or more, it returns the last two parts.

So, I pass in 127.0.0.1, it returns 127.0.0.1
If I pass in localhost, it returns localhost
If I pass in it returns google.com

I would just paste the methods in, but I used them in code written for my employer and don't want to get in trouble for posting it up here.

Unless someone has a better way to do it programmatically, you should just be able to easily implement what I've suggested here.

Let me know if you have any questions or problems.


----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
Thanks for all the help. It's getting to be quite tricky, especially since you can't simply just count the array element when spliting the host by periods. The reason is for cases like which has 3 periods. How would you know which one is the whois domain name when also accepting You can mistaken the domain as being co.uk and the "mydomain" as the subdomain or host.
 
I hope I'm on the right track. My intention was to point multiple websites to the same web application. Then in web.config, giving each website their own ConfigSection so I can tweak each website separately while still hitting the same app. I was going to differentiate each ConfigSection by assigning it's name as the whois domain name, since those are 100% unique. Also by grabbing this from the client request, it will know which ConfigSection to use automatically. What do you think about this logic? It's the beginning stages of a web app that will be used in clustered web farms, so I want to make sure this is a good, scalable method.

Trying to predict the numerous ways a user would enter a site can be a slippery slope, especially for a public site. Here's how I plan to tackle the situation as a start (using substring search):

public static class Globals
{
public readonly static SiteSettings Demo = (SiteSettings)WebConfigurationManager.GetSection("demo");
public readonly static SiteSettings GlobalID1 = (SiteSettings)WebConfigurationManager.GetSection("mydomain1.com");
public readonly static SiteSettings GlobalID2 = (SiteSettings)WebConfigurationManager.GetSection("testco.net");
public readonly static SiteSettings GlobalID3 = (SiteSettings)WebConfigurationManager.GetSection("mydomain.co.uk");
public readonly static SiteSettings GlobalID4 = (SiteSettings)WebConfigurationManager.GetSection("mydomain-xyz.ca");

public static SiteSettings Settings(string domain)
{
try
{
domain = domain.ToString().ToLower();
}
catch
{
return Demo;
}

if (domain.IndexOf("mydomain1.com") > 0)
{
return GlobalID1;
}
else if (domain.IndexOf("testco.net") > 0)
{
return GlobalID2;
}
else if (domain.IndexOf("mydomain.co.uk") > 0)
{
return GlobalID3;
}
else if (domain.IndexOf("mydomain-xyz.ca") > 0)
{
return GlobalID4;
}
else
{
return Demo;
}
}
}

This way, in any aspx page I can get the settings like this:
if (Session["Site"] == null)
Session["Site"] = Request.Url.DnsSafeHost;
Globals.Settings(Session["Site"]).UserFiles.Path

I still have to figure cases for IP addressed websites though.

In the web.config file, I want to put high level configurations in there such as connection, caching, user folder, etc. To give perspective on my app, here's my web.config section:
<configSections>
<section name="mydomain1.com" type="SiteSettings, __code"/>
<section name="testco.net" type="SiteSettings, __code"/>
<section name="mydomain.co.uk" type="SiteSettings, __code"/>
<section name="mydomain-xyz.ca" type="SiteSettings, __code"/>
</configSections>

<mydomain1.com defaultConnectionStringName="" providerType="" defaultCacheDuration="10">
<userFiles path="~/UserFiles/mydomain1" />
<settingsTable connectionStringName="WebConfig_Access" providerType="Company.DAL.MdbClient" tableName="MyDomain1Config" cacheDuration="0" />
...
</mydomain1.com>
...
<mydomain-xyz.ca defaultConnectionStringName="" providerType="" defaultCacheDuration="10">
<userFiles path="~/UserFiles/mydomain-xyz" />
<settingsTable connectionStringName="WebConfig_Oracle" providerType="Company.DAL.OracleClient" tableName="MyDomainXYZConfig" cacheDuration="0" />
...
</mydomain-xyz.ca>

Still got a long way, but a solid foundation goes even a longer way. I'm open to any suggestions or feedback.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top