Поделиться через


Redirecting in a Script Using the HttpRedirect Metabase Property

The HttpRedirect metabase property can be used to redirect client requests from a virtual directory, a Web directory, or a file, to either a resource on the same Web server or to a different URL.

For detailed syntax information about redirect variables and wildcards, seethe "Remarks" section in the HttpRedirect topic.

Redirecting to a URL

A simple redirection string uses the following format:

http://DestinationURL, Flag

In this format, DestinationURL can optionally include redirect variables to pass portions of the original URL with the destination URL.

Example Code

The following code example shows you how to use the Visual Basic Scripting Edition (VBScript) with ADSI to programmatically configure IIS to redirect requests from one virtual directory to another. The file name in the original request is appended to the destination URL by the $S variable and the EXACT_DESTINATION flag. Parameters are preserved by the $Q variable.

Set IIsWebVirtualDirObj = GetObject("IIS://localhost/W3SVC/1/Root/Scripts") 

IIsWebVirtualDirObj.Put "HttpRedirect", _ 
    "http://www.fabrikam.com/NewScripts$S$Q, EXACT_DESTINATION" 
IIsWebVirtualDirObj.SetInfo 

IIsWebVirtualDirObj.GetInfo 
WScript.Echo("Verify: HttpRedirect = " & IIsWebVirtualDirObj.HttpRedirect)

var providerObj = GetObject("winmgmts://MyServer/root/MicrosoftIISv2"); 
var IIsWebVirtualDirObj =  
    providerObj.get("IIsWebVirtualDirSetting='W3SVC/1/Root/Scripts'"); 

IIsWebVirtualDirObj.Properties_("HttpRedirect").Value =  
    "http://www.fabrikam.com/NewScripts$S$Q, EXACT_DESTINATION"; 
IIsWebVirtualDirObj.Put_(); 

IIsWebVirtualDirObj.get; 
WScript.Echo("Verify: HttpRedirect = " + IIsWebVirtualDirObj.HttpRedirect);

Redirecting Using Wildcards

A more complex redirection string uses one of the the following formats:

*; Wildcard1; Destination1[; Wildcard2; Destination2][, Flags]

*; !

In this format, WildcardN can include an asterisk (*)to match any number of characters in the original URL and pass them to DestinationN by using $0 through $9.

Example Code

The following example shows you how to use JScript with ADSI to programmatically configure IIS to redirect requests from one virtual directory to another. This example exempts the Web file called /Scripts/Policies.stm (phase 1). ASP files and DLLs in the /Transactions folder are redirected to different folders.

On Error Resume Next 

Set IIsWebVirtualDirObj = GetObject("IIS://localhost/W3SVC/1/Root/Scripts") 
If Err <> 0 Then  
    WScript.Echo Hex(Err.Number) & ": " & Err.Description 
    WScript.Echo "Error connecting to Scripts." 
    WScript.Quit(1) 
End If 

' The following code block assumes that  
' no metabase path for Policies.stm existed before. 
Set IIsWebFileObj = IIsWebVirtualDirObj.Create("IIsWebFile", "Policies.stm") 
IIsWebFileObj.Put "AccessFlags", IIsWebVirtualDirObj.AccessFlags 
IIsWebFileObj.SetInfo 
If Err <> 0 Then  
    WScript.Echo Hex(Err.Number) & ": " & Err.Description 
    WScript.Echo "Error connecting to Policies.stm." 
    WScript.Quit(1) 
End If 

IIsWebVirtualDirObj.Put "HttpRedirect", "http://www.fabrikam.com/NewScripts" 
IIsWebVirtualDirObj.SetInfo 

IIsWebFileObj.Put "HttpRedirect", "*;!" 
IIsWebFileObj.SetInfo 
WScript.Echo "Done phase 1." 

Set IIsWebVirtualDirObj = _ 
    GetObject("IIS://localhost/W3SVC/1/Root/Transactions") 
If Err <> 0 Then  
    WScript.Echo Hex(Err.Number) & ": " & Err.Description 
    WScript.Echo "Error connecting to Transactions." 
    WScript.Quit(1) 
End If 

Dim sRedir 
sRedir = "*; /Transactions/*.asp; http://www.fabrikam.com/ASP/%0.asp; " 
sRedir = sRedir & "/Transactions/*.dll; http://www.fabrikam.com/DLL/%0.dll" 
sRedir = sRedir & "EXACT_DESTINATION" 
IIsWebVirtualDirObj.Put "HttpRedirect", sRedir 
IIsWebVirtualDirObj.SetInfo 
WScript.Echo "Done phase 2."

Removing a Redirect

To remove a redirect, the HttpRedirect metabase property must be deleted. IIS returns a "The page cannot be displayed" error if the HttpRedirect metabase property is set to an empty string.

Example Code

The following code example shows you how to use the Visual Basic Scripting Edition (VBScript) with ADSI to programmatically remove the HttpRedirect metabase property from the W3SVC/1/Root/Scripts metabase path.

Const ADS_PROPERTY_CLEAR = 1 

Set IIsWebVirtualDirObj = GetObject("IIS://localhost/W3SVC/1/Root/Scripts") 
IIsWebVirtualDirObj.PutEx ADS_PROPERTY_CLEAR, "HttpRedirect", null 
IIsWebVirtualDirObj.SetInfo 

IIsWebVirtualDirObj.GetInfo 
WScript.Echo("Verify: HttpRedirect = " & IIsWebVirtualDirObj.HttpRedirect)