VBScript code for setting HttpRedirect Property for a IIsWebFile programmatically

I was helping one of my colleague whose customer wanted a custom VBScript file which sets redirection for certain files under a virtual directory. He wanted to use VBScript alone to achieve this.

A IIsWebFile node will be created in the metabase only when you specifically set a property for the file like HttpRedirect through the IIS manager UI. The below script, gets the IIsWebVirtualDir object of the virtual directory we are interested in, creates a IIsWebFile node and sets it HttpRedirect property:

' Virtual Directory under which you need to create the IIsWebFile
vDirName = "IIS://localhost/w3svc/1/root/artinstitutes"
' Getting the Virtual Directory (IIsWebVirDir object)
Set IIsWebVirtualDirObj = GetObject(vDirName)
' Call the CreateAWebFile function and pass the IIsWebVirDir object we have with the file name and the redirect url
CreateAWebFile(IIsWebVirtualDirObj, "test.html", "/check.asp?id=0")
CreateAWebFile(IIsWebVirtualDirObj, "test1.html","/check.asp?id=1”)
' Commit the changes to the Metabase
' This function takes 3 arguments
' 1 = IISObject – the container object
' 2 = filename – physical FileName which needs to be redirected
' 3 = redirectFileName – redirect URL
Function CreateAWebFile(ByVal IISObject, ByVal fileName, ByVal redirectName)
    Set myFileCheck = GetObject(vDirname & "/" & fileName)
    If (Err.Number <> 0) Then
        Set myFile = IISObject.Create("IIsWebFile", fileName)
    End If

myFile.HttpRedirect = redirectName & " ,PERMANENT"


End Function

Hope this helps!

Skip to main content