| This Issue In this issue we tackle a common problem of redirecting a lost user with an ISAPI Filter Extension. Here is the scenario, a user visits a site, likes it and adds the site to his/her favorites, later they return to find the page gone. The situation that we want to prevent is the user seeing a "HTTP/1.0 404 Object Not Found." This situation arises with site redesigns where pages are removed, most content providers do this without really thinking of the circumstances. A common solution is too keep the page in place, but make it only a link to the correct URL. Using the 404Redirect this situation can be prevented in a clean simple fashion with out extra pages. The Workings 404Redirect.dll redirects lost users to directories above the one that they are requesting. So for example if a user asks for a page like this: http://www.mydomain.com/gooddir/badpage.htm
Then they would be redirected to http://www.mydomain.com/gooddir/
Where normally, they would have gotten an 404 error. Recursion Using the example above as a base let's try a harder example. If the user enters a page like this: http://www.mydomain.com/goodir/baddir/badpage.htm
They will be redirected to http://www.mydomain.com/goodir/baddir/ But, this is also a bad request, so when the browser makes this request it gets redirected to: http://www.mydomain.com/goodir/
The filter recursively gets called for every bad request until it reaches the root of the server. All server should have a default page on the root. The Code Both the code and the compiled filter are both available to download. Requirements The 404 Redirect can run on any Windows based web server that supports ISAPI Filters. It was design to run on Internet Information Server 1.0 or better. To compile the code you will need to have Microsoft Developer Studio 4.2 or better. The Code Here is both the code and a release version of the DLL. The code is fully commented so that not only do you have the source, but can understand how it works. You will need WinZip to extract the zip file since most of the file names are longer then 8.3. Allocating Memory on the Heap In both ISAPI Server Extensions and ISAPI Server Filters it is consider bad practice to allocate memory on the heap, since it slows down performance. All memory for strings and alike should be allocated on the stack. This is sometimes limiting since the length of string might not be known. A example of this is demonstrated with lpszURL. lpszURL is a pointer to the start of a string which contains a URL of unknown length. In the 404Redirect code we set the maximum length of lpszURL at 255 and allocate the memory on the stack. We can get away with this because on the circumstances. It is better to limit the ISAPI Extensions for some users then to slow down all the users for the extreme cases. Quick String Parsing and Comparing Since this type of Filter handles all outgoing data it is important that the majority of the responses are processed quickly. In 404 Redirect the code is only looking for HTTP/1.0 404. The Majority of the responses are non 404 responses, these only have to under go one string compare. Once the responses are separated into 404s and non 404s ( by the one string compare) then we take our time handling the 404 responses. The non 404s exit out of the filter after the string compare to go along their merry way. Installing the Filter on the IIS Machine Follow the instructions below to add 404Redirect to your server. - Copy 404Redirect.dll to c:\winnt\system32\inetsrv.
- Stop the IIS Web Server using the Internet Service Manager.
- Select Start | Run from the task bar of NT.
- Run regedit.exe
- Go to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W3SVC\Parameters .
- Add c:\winnt\system32\inetsrv\404Redirect.dll to the Filter DLLs value. It is important that you add the DLL to the end of this comma delimited list. Do not remove the other entries.
- Start the IIS Web Server up using the Internet Service Manager
15 Seconds Thanks for the support. Our last issue (Connection Caching) got read by thousands. Most came to read how to use connection caching with ODBC 3.0. Others where looking for some tricks on how to do user interaction. What ever the reason many came and downloaded the connection caching code. This issue (Redirecting Filter), we hope will be more popular, since both developers and system administrators alike can use the code to directly improve their web site. Now that we have the second issue out the door, we will start looking for sponsors. Currently, we are running banners at the bottom of the page to advertise ourselves. However, we need to take on sponsors to fund our efforts. Since the readers are mostly developers and system administrators of the Internet Information Server, the viewing audience in narrow. This is a good market for development companies and tool makers. The next issue will show how to implement the popular sendmail Unix CGI script using Active Server Pages. Send mail allows you to convert information collected in a form to email, The email is then sent to a specific individual, by the server. The next issue will contain all the source code and a detailed explanation of how the Active Server sendmail works. Thanks, 15 Seconds Editing Staff
|