Partilhar via


Response.Redirect Method

The Redirect method causes the browser to redirect the client to a different URL.

Redirect(
      URL
)

Parameters

  • URL
    The Uniform Resource Locator (URL) that the browser is redirected to. This can be an full URL beginning with "http://", a virtual path to a location on the same IIS server, or the name of a file contained in the same location as the original URL.

    URL can include a query string.

    Older Web browsers might convert a POST request to a GET request during a redirection.

    Caution

    Always validate and encode the URL that is passed to Response.Redirect to protect against cross-site scripting attacks. For information about how to remove harmful characters from strings, see Removing Harmful Characters from User Input.

Return Values

This method has no return values.

Applies To

Response Object

Remarks

Any response body content such as displayed HTML text or Response.Write text in the page indicated by the original URL is ignored. However, this method does send other HTTP headers set by this page indicated by the original URL to the client. An automatic response body containing the redirect URL as a link is generated. The Redirect method sends the following explicit header, where URL is the value passed to the method, as shown in the following code:

HTTP 1.0 302 Object Moved 
Location  
https://www.microsoft.com 

Example Code

The following example shows you how to use the VBScript programming language to redirect the user to the Microsoft Web site after validating the URL.

<%@ LANGUAGE="VBScript" %> 
<% 
  Dim MyUrl = "https://www.microsoft.com" 

  Response.CodePage = 1252 
  If ValidateInput(MyUrl) Then   
    Response.Redirect (myURL) 
  Else 
    Response.Write("URL was invalid.") 
  End If 

  Function ValidateInput(sInput) 
    Dim reValid 
    Set reValid = New RegExp 

    reValid.Pattern = "^[\w\.:\?&=/]*$" 
    reValid.MultiLine = False 
    reValid.Global = True 

    ValidateInput = reValid.Test(sInput) 
  End Function 
%>

The following example shows you how to use the VBScript programming language to redirect the user to a virtual directory on the same IIS server.

<% Response.Redirect "/samples/asp/newpage.asp" %> 

The following example shows you how to use the VBScript programming language to redirect the user to a local file while passing a query string.

<% Response.Redirect Server.HTMLEncode("newpage.asp?var1=5&var2=7") %> 

Form and query string data is not transferred to the new URL. The following example shows you how to use the VBScript programming language to pass a querystring from the original request to the new URL.

<% 
dim qs 
qs = Server.URLEncode(Request.Querystring) 
Response.Redirect "newpage.asp?" + Server.HTMLEncode(qs) 
%> 

Requirements

Client: Requires Windows XP Professional, Windows 2000 Professional, or Windows NT Workstation 4.0.

Server: Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server 4.0.

Product: IIS

See Also