<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.iis.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:cs="http://blogs.iis.net/"><channel><title>rakkimk : ISAPI</title><link>http://blogs.iis.net/rakkimk/archive/tags/ISAPI/default.aspx</link><description>Tags: ISAPI</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>IIS - Sample ISAPI Filter doing Redirection to another website</title><link>http://blogs.iis.net/rakkimk/archive/2008/05/09/iis-sample-isapi-filter-doing-redirection-to-another-website.aspx</link><pubDate>Fri, 09 May 2008 23:17:00 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:2349266</guid><dc:creator>rakkimk</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.iis.net/rakkimk/rsscomments.aspx?PostID=2349266</wfw:commentRss><comments>http://blogs.iis.net/rakkimk/archive/2008/05/09/iis-sample-isapi-filter-doing-redirection-to-another-website.aspx#comments</comments><description>&lt;P&gt;I know I'm in a very old world of writing ISAPI Filters to do the redirection instead of just creating an IHttpModule and plug it directly in the IIS7 request pipeline. But, one of my customer wanted this ISAPI filter and I made a fairly simple ISAPI Filter to do the redirection. &lt;/P&gt;
&lt;P&gt;Below sample doesn't do any checks, or maintains any lists of mapped URLs, but just a simple redirection of all the requests to &lt;A href="http://www.live.com/" mce_href="http://www.live.com"&gt;http://www.live.com&lt;/A&gt;. Feel free to modify it accommodating your need. &lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #2e595c 1px solid; PADDING-RIGHT: 0px; BORDER-TOP: #2e595c 1px solid; PADDING-LEFT: 0px; BACKGROUND: #fff; LEFT: -2px; PADDING-BOTTOM: 0px; MARGIN-LEFT: 4px; BORDER-LEFT: #2e595c 1px solid; COLOR: black; MARGIN-RIGHT: 4px; PADDING-TOP: 0px; BORDER-BOTTOM: #2e595c 1px solid; POSITION: relative; TOP: -2px"&gt;
&lt;DIV style="BORDER-RIGHT: blue 1px solid; BORDER-TOP: blue 1px solid; BACKGROUND: #3f73b6; BORDER-LEFT: blue 1px solid; WIDTH: 100%; COLOR: white; BORDER-BOTTOM: blue 1px solid"&gt;&lt;SPAN style="WIDTH: 100%"&gt;Code Snippet&lt;/SPAN&gt;&lt;/DIV&gt;&lt;PRE&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;#include&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #00ff00"&gt;&amp;lt;stdio.h&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; 
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;#include&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #00ff00"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; 
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;#include&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #00ff00"&gt;&amp;lt;afx.h&amp;gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;#include&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #00ff00"&gt;&amp;lt;afxisapi.h&amp;gt;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;BOOL WINAPI &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;__stdcall&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; GetFilterVersion(HTTP_FILTER_VERSION *pVer) 
{ 
  pVer-&amp;gt;dwFlags = (SF_NOTIFY_PREPROC_HEADERS ); 
  CFile myFile(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #00ff00"&gt;"C:\\ISAPILOG\\URLs.html"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;, CFile::modeCreate);
  myFile.Close();
  pVer-&amp;gt;dwFilterVersion = HTTP_FILTER_REVISION; 
  strcpy(pVer-&amp;gt;lpszFilterDesc, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #00ff00"&gt;"Sample Redirection ISAPI"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;); 
  &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; TRUE; 
} 

DWORD WINAPI &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;__stdcall&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; HttpFilterProc(HTTP_FILTER_CONTEXT *pfc, DWORD NotificationType, VOID *pvData) 
{ 
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;   char&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; buffer[256];
   DWORD buffSize = &lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;sizeof&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;(buffer);
   HTTP_FILTER_PREPROC_HEADERS *p;
   CHttpFilterContext *chc;
   chc = (CHttpFilterContext *)pfc;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;   char&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; *newUrl;
   CFile myFile(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #00ff00"&gt;"C:\\ISAPILOG\\URLs.html"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;, CFile::modeWrite);

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;   switch&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; (NotificationType)  { 

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;   case&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; SF_NOTIFY_PREPROC_HEADERS :

   p = (HTTP_FILTER_PREPROC_HEADERS *)pvData;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;   char&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; newUrl[50];
   wsprintf(newUrl, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #00ff00"&gt;"http://www.live.com/"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;);

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;   char&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; szTemp[50];
   wsprintf(szTemp, &lt;/SPAN&gt;&lt;SPAN style="COLOR: #00ff00"&gt;"Location: %s\r\n\r\n"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;,newUrl);

   pfc-&amp;gt;ServerSupportFunction (pfc,
                            SF_REQ_SEND_RESPONSE_HEADER,
                            (PVOID) &lt;/SPAN&gt;&lt;SPAN style="COLOR: #00ff00"&gt;"302 Redirect"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;,
                            (DWORD) szTemp,0); 

   myFile.SeekToEnd();
   myFile.Write(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #00ff00"&gt;"&amp;lt;BR&amp;gt;&amp;lt;B&amp;gt; Orignial URL : &amp;lt;/B&amp;gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;,strlen(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #00ff00"&gt;"&amp;lt;BR&amp;gt;&amp;lt;B&amp;gt; Orignial URL : &amp;lt;/B&amp;gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;));
   BOOL bHeader = p-&amp;gt;GetHeader(pfc,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #00ff00"&gt;"url"&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;,buffer,&amp;amp;buffSize); 
   CString myURL(buffer);
   myURL.MakeLower(); 
   myFile.Write(buffer,buffSize);
   
   myFile.Write(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #00ff00"&gt;" &amp;lt;B&amp;gt;New URL : &amp;lt;/B&amp;gt; "&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;,strlen(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #00ff00"&gt;" &amp;lt;B&amp;gt;New URL : &amp;lt;/B&amp;gt; "&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;));
   myFile.Write(newUrl,strlen(newUrl));
   myFile.Close();

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;   return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; SF_STATUS_REQ_HANDLED_NOTIFICATION; 
   }
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;  return&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt; SF_STATUS_REQ_NEXT_NOTIFICATION; 
} &lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;FONT color=#000000&gt;Above is my sample, You might want to check my &lt;A href="http://blogs.msdn.com/rakkimk/archive/2007/03/01/writing-a-simple-isapi-filter.aspx" mce_href="http://blogs.msdn.com/rakkimk/archive/2007/03/01/writing-a-simple-isapi-filter.aspx"&gt;earlier ISAPI blog post&lt;/A&gt; to get the .def file and steps to create the DLL. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#000000&gt;&lt;BR&gt;Hope this helps!&lt;/FONT&gt; 
&lt;P&gt;&lt;FONT style="BACKGROUND-COLOR: #808080"&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/SPAN&gt;&lt;img src="http://blogs.iis.net/aggbug.aspx?PostID=2349266" width="1" height="1"&gt;</description><category domain="http://blogs.iis.net/rakkimk/archive/tags/ISAPI/default.aspx">ISAPI</category><category domain="http://blogs.iis.net/rakkimk/archive/tags/IIS/default.aspx">IIS</category></item></channel></rss>