Redirection Extension

Overview

This sample demonstrates how to create an ISAPI extension that redirects requests made to the server. The new target URL is provided to the extension through the query string; if the URL is not provided, or is not legal, an error message is sent to the client browser, along with simple usage instructions. Thus, if http://mysite.com/scripts/Redirect.dll was the URL of the compiled DLL for this extension, the following request,

http://mysite.com/scripts/Redirect.dll?//anothersite.com/default.htm 

would cause the browser to look to anothersite.com for its next page.

Code Tour

This example implements the standard extension entry-point functions, GetExtensionVersion and HttpExtensionProc. The latter uses the pointer passed by IIS to access the lpszQueryString member of the ECB, and then parses the query to determine whether the client browser is requesting a remote or local resource. Requests for remote redirection, such as http://othersite.com, are processed by sending the HSE_REQ_SEND_URL_REDIRECT_RESP extension request to the server with the ServerSupportFunction. HSE_REQ_SEND_URL_REDIRECT_RESP indicates that the server is to send the HTTP status code 302, and redirection message, to the client browser.

If the query string is found to contain a virtual path, then ServerSupportFunction is used, this time invoking the HSE_REQ_SEND_URL extension request. The server now sends the file at the indicated new target location, while the extension exits. Note that the sample explicitly checks for the leading forward slash (/) in the virtual path, as the HSE_REQ_SEND_URL extension request of the ServerSupportFunction requires it, and will not work properly if the forward slash is absent.

Location

This project is available in the ...\isapi\extensions\redirect subdirectory of the IIS samples directory.