Domain & subdomain url redirection with file/folder security.
By: Sergeroz
Language: Visual Basic Script
Code Snippet:
This code implements use of the Global.asa file and 1 #include file,
which should be included in all pages you wish to protect.
FILES USED: GLOBAL.ASA, DOMAINCHECK.INC, 403.ASP
Global.asa file detects which domain name your visitors typed in
(using SERVER_NAME ServerVariable), and sets the DomainNum Session variable
accordingly.
DomainCheck.Inc must be included in ALL webpages you wish to "protect" like this:
<!-- #include virtual="domaincheck.inc" -->
This file contains code that will check whether visitors are allowed to visit the current URL
and will deny access or redirect accordingly.
403.asp is used to report Access Denied for protected URLs. It may be formatted any way
you wish.
Sample Scenario 1:
A visitor navigates to www.domain1.com. IIS will first execute Global.asa and will then
execute index.asp (default page).
New Session is created: DomainNum = 1, NewSession = True.
Index.asp contains the #include to domaincheck.inc, so rules are applied before executing
the remainder of index.asp:
DenyPath is matched with the '/' entry and ThisURL = HomeURL, thus DoRedirect is called.
HomeRedirURL is not blank, NewSession = True and ThisURL = HomeURL,
so the visitor is redirected to '/domain1only/' directory.
Now, index.asp containing the same #include is found in the /domain1only/ directory and
domaincheck.inc is executed again:
DenyPath is not matched (because although '/' entry matches, ThisURL is not HomeURL,
thus DoRedirect not called.
AllowOnlyPath is blank so this check is skipped.
NewSession is set to False.
Thus, the visitor has now been transparently transferred to the directory containing the
domain1.com website.
Sample Scenario 2:
A visitor navigates to www.domain2.com/domain1only/. IIS will first execute Global.asa
and will then execute index.asp (default page).
New Session is created: DomainNum = 2, NewSession = True. Index.asp contains the
#include to domaincheck.inc, so rules are applied before executing the remainder of
index.asp:
DenyPath is blank so this check is skipped.
AllowOnlyPath is not matched so DoRedirect is called.
HomeRedirURL is not blank, NewSession = True, so the visitor is redirected
to '/domain2only/' directory.
Now, index.asp containing the same #include is found in the /domain2only/ directory and
domaincheck.inc is executed again:
DenyPath is blank so this check is skipped.
AllowOnlyPath is matched so DoRedirect is not called.
NewSession is set to False.
Thus, the visitor has now been transparently transferred to the directory containing the
domain2.com website.
Sample Scenario 3:
Repeat Sample Scenario 1, then continue:
The visitor now navigates to www.domain1.com/domain2only/ (assuming the Session has not
yet timed out). Global.asa will not be executed because a current Session is alive
(DomainNum = 1, NewSession = False). Index.asp in the /domain2only/ directory contains
the #include to domaincheck.inc, so rules are applied before executing the remainder of
index.asp:
DenyPath is matched with the '/domain2only/' entry, thus DoRedirect is called.
AllowOnlyPath is not blank so this check is skipped.
HomeRedirURL is not blank, but NewSession = False and ThisURL is not HomeURL, so the
visitor is redirected to '/403.asp?/domain2only/index.asp'. This page informs the user that
he is not allowed to access /domain2only/index.asp.
PASTE THE CODE BELOW INTO YOUR GLOBAL.ASA FILE (CREATE ONE, IF NEEDED)
PASTE THE CODE BELOW INTO YOUR DOMAINCHECK.INC FILE (CREATE ONE, IF NEEDED)