• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

Virtual Subdomain

G

Glen

Guest
How do I create a situation that any subdomain redirects to the main domain? We already have a wildcard in our DNS but anything.mysite.com displays the default plesk page instead of www.mysite.com. I'm looking for a solution that isn't destroyed if I change something else with plesk and that works for all domains that I host without having to have a separate IP address for every domain.
 
Hi Glen,

Directly within IIS you can create a 'default' site (one that responds whenever a site is not aliased to it) to run on all ip addresses. Then create a default.asp file in the folder which you've mapped that site to which reads something like this (you might have to play with it):

<%

'[Settings]
sQD = Replace(Request.ServerVariables("SERVER_NAME"), "'", "''")
sQD = Replace(sQD, "\", "\\")

' get just the FQDN from the server name
Dim rv
Set rv = New RegExp
rv.Global = True
rv.MultiLine = True
rv.IgnoreCase = True
rv.Pattern = "([a-zA-Z0-9\-\_]*\.[a-zA-Z]*)$"
If rv.Test(sQD) Then sQD = rv.Execute(sQD)(0).Value
Set rv = Nothing

' redirect
Response.Redirect "http://" & sQD & "/"
%>
 
Hi Shal,

Tnx. In your solution, what is displayed in the address bar? Is it 'anything.mysite.com' or 'www.mysite.com'?

Reading back my own post, I think I have not been so clear (sorry for that). What I want is the address bar to display the subdomain and arrange what is displayed in the browser using ISAPI-rewrite. So 'anything.mysite.com' is in the address bar but the browser displays 'www.mysite.com/?user=anything'. The problem with ISAPI-rewrite seems to be that it doesn't catch the requests because the subdomains land on the 'default site'.

With what you describe, I think I don't need ISAPI-rewrite at all because I could tune the redirect script to create the url 'www.mysite.com/?user=anything'. But it all depends on whether the original subdomain stays in the address bar or not.
 
Well, that's different...

Hi Glen,

Ah, so you need to preserve the extra text from the subdomain. What you preserve in the address bar could be a few different things. You could have it create a frameset or redirect to the querystring URL. The code above doesn't do that, it just redirects to the root page. You're probably looking for something closer to this:

Code:
<%

'[Settings]
  bDisplay = 1 ' use 1 for frameset, 0 for redirect
  sTag = ""    ' default tag

'[Parse]
  sQD = Replace(Request.ServerVariables("SERVER_NAME"), "'", "''")
  sQD = Replace(sQD, "\", "\\")

' get just the FQDN from the server name
  Dim rv
  Set rv = New RegExp
  rv.Global = True
  rv.MultiLine = True
  rv.IgnoreCase = True
  rv.Pattern = "([a-zA-Z0-9\-\_]*\.[a-zA-Z]*)$"
  If rv.Test(sQD) Then
    sDom = rv.Execute(sQD)(0).Value
    sTag = Replace(sQD, sDom, "")
  End If
  Set rv = Nothing

'[Redirect]
  sUrl = "http://" & sDom & "/?user=" & sTag
  If bDisplay = 0 Then
  ' straight redirect
  ' replaces "joe.example.com" with "example.com/?user=joe"
    Response.Redirect sUrl
  Else
  ' frame
  ' preserves "joe.example.com" within address bar
    Response.Write "<html>" & vbCrLf
    Response.Write "<frameset cols=""100%"">" & vbCrLf
    Response.Write "  <frame src=""" & sUrl & """ />" & vbCrLf
    Response.Write "</frameset>" & vbCrLf
    Response.Write "</html>"
  End If

%>
That'll do what you're after. Change the "bDisplay" value to toggle how it works. Right now (1) it's setup to frame the new page so the original URL is preserved in the address bar.
 
Back
Top