views:

603

answers:

4

I need to extract only the 2nd level part of the domain from request.servervariables("HTTP_HOST") what is the best way to do this?

A: 
If Len(strHostDomain) > 0 Then   
    aryDomain = Split(strHostDomain,".")

    If uBound(aryDomain) >= 1 Then
     str2ndLevel = aryDomain(uBound(aryDomain)-1)
     strTopLevel = aryDomain(uBound(aryDomain))   
     strDomainOnly = str2ndLevel & "." & strTopLevel
    End If
End If

works for what I need but it doesn't handle .co.uk or other domains that have two parts expected for the top level.

Brian Boatright
+1  A: 

This requires the current version of Scripting Host to work (you will have it very likely, though):

Set re = New RegExp

re.Pattern = "^(.*?)\.?([^.]+)\.(\w{2,}|\w{2}\.\w{2})$"

Set matches = re.Execute( Request.ServerVariables("HTTP_HOST") )

If matches.count = 1 Then
  Set match = matches(0)

  ' assume "images.res.somedomain.co.uk" '
  Response.Write match.SubMatches(0) & "<br>" ' will be "images.res" '
  Response.Write match.SubMatches(1) & "<br>" ' will be "somedomain" '
  Response.Write match.SubMatches(2) & "<br>" ' will be "co.uk"      '

  ' assume "somedomain.com" '
  Response.Write match.SubMatches(0) & "<br>" ' will be ""           '
  Response.Write match.SubMatches(1) & "<br>" ' will be "somedomain" '
  Response.Write match.SubMatches(2) & "<br>" ' will be "com"        '
Else
 ' You have an IP address in HTTP_HOST '
End If

Obviously, the SO code formatter does not know VBScript, so this is badly colorized.

Tomalak
The HTPT_HOST header isn't supposed to include sub-domains. This is nonsense. To parse the subdomain, he would need to use the SERVER_NAME header.Given the url: http://cdn.domain.co.uk, HTTP_HOST should return "domain.co.uk", while SERVER_NAME should return "cdn.domain.co.uk"
defeated
Of course does HTTP_HOST contain sub domains. Go look it up.
Tomalak
like this? http://www.google.com/search?q=http_host+vs+server_name
defeated
A: 

Since HTTP_HOST header only returns the domain (excluding any subdomains), you should be able to do the following:

'example: sample.com
'example: sample.co.uk
host = split(request.serverVariables("HTTP_HOST"), ".")
host(0) = "" 'clear the "sample" part

extension = join(host, ".") 'put it back together, ".com" or ".co.uk"
defeated
This is nonsense. I don't think you understood the question.
Tomalak
A: 

Just checked the difference on a subdomain off my rented serverspace and both http_host and server_name reported back the domain name including the subdomain.

Haakon