Click here to Skip to main content
15,174,684 members
Articles / Desktop Programming / MFC
Posted 10 Sep 2003


52 bookmarked

How to Make an ISAPI Redirection Filter

Rate me:
Please Sign up or sign in to vote.
4.36/5 (20 votes)
10 Sep 20034 min read
How to build a simple ISAPI Filter


Redirecting web traffic is a fairly common task, we can redirect from HTML, from ASP (or other scripting engines) and from ISAPI. This tutorial will demonstrate how to reduce server workload by redirecting from ISAPI using MFC using a “redirection filter”. This example will redirect a requested page, while retaining the URL the user sees in their browser.

Html Redirect

The code to redirect a page looks something like the following:

<META http-equiv="refresh" content="5; URL=">

ASP Redirect

The ASP code to redirect looks like:

response.redirect ""

The first relies on you having each page set up to redirect, the second is done via ASP which means it requires use of the scripting engine (server hit).

ISAPI Redirection Filter

We can reduce the server load by using ISAPI filters in C++. This will have the same effect – the downside is that you need to know how to use Visual Studio to do it. The upside is that you don’t use ASP. This filter will change the URL of the file IIS thinks it wants to return, without changing the URL the user sees in their browser.

The Project

Open Visual Studio (I use version 6.0). Make a new project and select “ISAPI Extension Wizard”.

New ISAPI Project

Give your project a name. I’m using “redirector”, click "OK". You should now be presented with the following:

Image 2

Tick “Generate Filter Object” and un-tick “Generate a Server Extension Object”. In case, I’ll also link statically.

Click Next...

Image 3

Because we only want to redirect the actual page and not the page the user thinks it is, we’ll tick “Post-processing of the request headers” and select the notification priority we want the filter to have, in this case, we’ll select “high” to make sure it gets attention before any other filters. Click “Finish” and Visual Studio will now prepare the project for you.

The Code

We will now add our code to redirect all requests from .cfm to .asp. The function of interest is in redirector.cpp and is called OnPreprocHeaders and it looks something like the following:

DWORD CRedirectorFilter::OnPreprocHeaders(
                             CHttpFilterContext* pCtxt, 
                             PHTTP_FILTER_PREPROC_HEADERS pHeaderInfo

This function is called when the server has preprocessed the client headers. In this example, we want to modify the URL that IIS thinks it wants to return. We’ll try something simple - replacing all Coldfusion (.cfm) extensions with .asp. Why? Perhaps we’re using ASP instead of ColdFusion and don’t want to change the links on the pages themselves.

We need to find out which page has been requested, and then replace .cfm with .asp if necessary. To do this, we need to use the HTTP_FILTER_PREPROC_HEADERS pointer, pHeaderInfo, and the filter context pointer pCtxt.

We want to see the URL requested, to do this, we can ask IIS what the URL was by way of the GetHeader function.

DWORD CRedirectorFilter::OnPreprocHeaders(
                             CHttpFilterContext* pCtxt, 
                             PHTTP_FILTER_PREPROC_HEADERS pHeaderInfo
    char buffer[256]
    DWORD buffSize = sizeof(buffer);
    BOOL  bHeader=pHeaderInfo->GetHeader(pCtxt->m_pFC, "url",
                                             buffer, &buffSize); 

Once we have the URL, we can decide whether to redirect it. I’m going to cheat a little here and use CString to deal with the string code, you can use whatever string code you like:

DWORD CRedirectorFilter::OnPreprocHeaders(
                             CHttpFilterContext* pCtxt,
                             PHTTP_FILTER_PREPROC_HEADERS pHeaderInfo
    char buffer[256];
    DWORD buffSize = sizeof(buffer);
    BOOL bHeader = pHeaderInfo->GetHeader(pCtxt->m_pFC, "url",
                                              buffer, &buffSize); 
    CString urlString(buffer);
    urlString.MakeLower(); // for this exercise 
    if (urlString.Find(".cfm") != -1) //we want to redirect this file
        char *newUrlString= urlString.GetBuffer(urlString.GetLength());
        pHeaderInfo->SetHeader(pCtxt->m_pFC, "url", newUrlString);
    //we want to leave this alone and let IIS handle it

Now, build the file and with any luck, there will be no errors!

Adding the Filter to IIS

Next, you will want to add the filter to IIS, or Personal Web Service if you are using it. I’m now going to describe how to add the filter to Personal Web Service.

So, go to Administrative tools (in Windows 2000) and select Internet Services Manger, something like the following will now appear:

Image 4

Right click on “Default Web Site” (or whatever web server you want to add it to) and select “properties”, the following windows should then appear.

Image 5

From the tabs, click on “ISAPI Filters”.

Image 6

Now, assuming there are no filters already present, the box in the middle should be clear (as this example shows) Click the “Add” button…

Image 7

Give your filter a “name”, and use the browse button to locate the redirection filter .dll file.

Image 8

Click “OK” and the filter will now appear, if you want your filter to have higher priority than others that may appear in the list, use the buttons on the left hand side to organize the correct order.

Image 9

Click “OK”. The tabbed dialog will disappear.

Next, we want to stop and start the web service. Right click on the “Default Web Site”, select “Stop” then right click again and select “Start”.

Alternatively, you can click the black square on the rebar to stop the service, and click the “Start item” (black triangle) to restart it.

Testing It

Make a file test.asp and place it in your wwwroot directory of inetpub. Open your web browser and open location http://localhost/test.cfm. If all goes well, then the contents of test.asp will be displayed in the browser.

That’s it, you’re done!


This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


About the Author

Software Developer (Senior) TMR
Australia Australia
No Biography provided

Comments and Discussions

GeneralRe: VSTS (VS 2005) Pin
Offlinesurfer15-Dec-06 4:58
MemberOfflinesurfer15-Dec-06 4:58 
GeneralRe: VSTS (VS 2005) Pin
bryce4-Jan-07 19:52
Memberbryce4-Jan-07 19:52 
GeneralRe: VSTS (VS 2005) Pin
Offlinesurfer9-Jan-07 3:50
MemberOfflinesurfer9-Jan-07 3:50 
GeneralRe: VSTS (VS 2005) Pin
YUserID3-Apr-07 5:23
MemberYUserID3-Apr-07 5:23 
QuestionAny filter to add a banner or footer to all static and dynamic pages? Pin
marcelo_imai23-Nov-06 5:08
Membermarcelo_imai23-Nov-06 5:08 
AnswerRe: Any filter to add a banner or footer to all static and dynamic pages? Pin
bryce23-Nov-06 11:54
Memberbryce23-Nov-06 11:54 
GeneralHi Bryce,how to Get unicode url and redirect Pin
seanleee24-Jun-06 2:07
Memberseanleee24-Jun-06 2:07 
GeneralISAPI Filter is not active Pin
VC Sekhar Parepalli4-Apr-06 10:17
MemberVC Sekhar Parepalli4-Apr-06 10:17 
Hi Bryce,
This is really a good looking for this one exactly. I have one small doubt. In my machine(am the admin user for it), I created the filter programmatically and now the filter is not enabled at that IIS > defaukt website>right click>properties> ISAPI Filters tab . neither the "enable" button is enabled. Could you please let me know if I missed anything?


QuestionI want to show the new URL to the browser. Pin
John T. Kelly8-Oct-05 13:59
sussJohn T. Kelly8-Oct-05 13:59 
Generalhelp me! How can I control speeds. Pin
10244225-Aug-05 20:46
Member10244225-Aug-05 20:46 
GeneralIs there some ISAPI Book Pin
Vasudevan Deepak Kumar19-Jul-05 3:03
MemberVasudevan Deepak Kumar19-Jul-05 3:03 
GeneralRedirecting across app pools Pin
Anonymous28-Mar-05 15:50
MemberAnonymous28-Mar-05 15:50 
Generalfull url required Pin
jagatnibas28-Nov-04 22:24
Memberjagatnibas28-Nov-04 22:24 
GeneralRe: full url required Pin
Anonymous24-Mar-05 7:22
MemberAnonymous24-Mar-05 7:22 
GeneralRe: full url required Pin
fbalicki29-Jun-06 5:54
Memberfbalicki29-Jun-06 5:54 
GeneralReading the IIS metabase Pin
jfprince14-Oct-04 1:24
Memberjfprince14-Oct-04 1:24 
GeneralThe browser sees the re write in URL as if it is a redirect Pin
Anonymous1-Oct-03 5:22
MemberAnonymous1-Oct-03 5:22 
GeneralRe: The browser sees the re write in URL as if it is a redirect Pin
Anonymous2-Oct-03 12:13
MemberAnonymous2-Oct-03 12:13 
GeneralRe: The browser sees the re write in URL as if it is a redirect Pin
sumit mahajan21-Mar-09 10:31
Membersumit mahajan21-Mar-09 10:31 
GeneralRe: The browser sees the re write in URL as if it is a redirect Pin
sumit mahajan24-Mar-09 12:08
Membersumit mahajan24-Mar-09 12:08 
GeneralBook Pin
e_tarek15-Sep-03 23:25
Membere_tarek15-Sep-03 23:25 Pin
Uwe Keim11-Sep-03 5:21
sitebuilderUwe Keim11-Sep-03 5:21 
GeneralFatal Error Pin
Andreas Saurwein11-Sep-03 4:19
MemberAndreas Saurwein11-Sep-03 4:19 
GeneralRe: Fatal Error Pin
Chris Maunder13-Sep-03 6:48
cofounderChris Maunder13-Sep-03 6:48 
GeneralRe: Fatal Error Pin
Andreas Saurwein13-Sep-03 8:52
MemberAndreas Saurwein13-Sep-03 8:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.