Export (0) Print
Expand All
31 out of 62 rated this helpful - Rate this topic

Response.Redirect Method

IIS 6.0

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

Redirect(
   URL
)
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 noteCaution:

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.

This method has no return values.

Any response body content such as displayed HTML text or Response.Write text in the page indicated by the original URL is ignored. In addition, code execution in the current page is terminated when the Redirect method is processed, so subsequent code in the page will also be 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: http://www.microsoft.com

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 
  MyUrl = "http://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) 
%> 


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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.