Classic ASP Active Server Pages Examples, code, tutorials, scriptsClassic ASP Script Custom 404.asp error page, can be adapted for "fake" URL rewriting

This script is an example of how to use a custom 404 error page to parse  imaginary, virtual, ghost folder urls and then send visitors to actual corresponding folders. For instance, if you have a folder of community user's home pages that has a URL such as http://whateveryourdomainis/members/username/ you can use http://whateveryourdomainis/username as a shortcut URL without having to actually create the folder because you could make http://whateveryourdomainis/members/username/ redirect to http://whateveryourdomainis/username/

It should be simple enough to adapt this method for Classic ASP 404 page-not-found error pages to specific needs and might be easier than messing with URL rewrite rules for IIS7 and web.config, when used with server.execute

DEMONSTRATION:
http://classicaspreference.com/404test/404sub/flaldkj.asp
http://classicaspreference.com/404sub/flaldkj.asp  (There is no folder 404sub in the root directory. I want visitors to http://classicaspreference.com/404test/404sub/ to be given "URL rewriting.")

 

Rated:
by Aspin.com users
What do you think?

 

UPDATED 12/25/2010 for WINDOWS SERVER 2008 & IIS7 (I use Arvixe.com NET hosting)



To use the updated version of this script, inside the system.webserver tags of your web.config file, you must have a set of lines something like the following lines in red:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="false"/>
<httpErrors>
<clear />
<error statusCode="404" subStatusCode="-1" path="/404.asp" responseMode="ExecuteURL" />
</httpErrors>

</system.webServer>
</configuration>

Note that the path MUST be relative, not absolute when using ExecuteURL.

Here is the updated script for 404.asp:

<%
'modify folder path, allowed folders array, and file to server.execute for your needs
strExeYES =1 ' strExeYes is for whether or not to show the error message inside div#1
'THE PAGE FILE HEADER TAGS:
Response.write("<html>" & vbNewline)
Response.write("<head>" & vbNewline)
Response.write("<meta http-equiv=""Content-Language"" content=""en-us"">" & vbNewline)
Response.write("<meta http-equiv=""Content-Type"" content=""text/html; charset=windows-1252"">" & vbNewline)
Response.write("<title>Classic ASP Reference 404 page not found</title>" & vbNewline)
Response.write("</head>" & vbNewline)
Response.write("<body background="""" leftmargin=""200"">" & vbNewline)
Response.write("<h2><font color=""navy"" face=""Verdana"">Classic ASP Reference</font></h2>" & vbNewline)

dim fname,strFolder,strDomain,sMyString,strHomeFolder

'MODIFY THE LINE BELOW WITH YOUR FOLDER PATH:
strHomeFolder="C:\YOURPATH\wwwroot\"
fname = Request.ServerVariables("SCRIPT_NAME")
strDomain = request.servervariables("SERVER_NAME")
'THE LINE BELOW ASSUMES THAT Request.ServerVariables("QUERY_STRING") WILL RETURN A VALUE LIKE 404;http://YOURDOMAIN.WHATEVER:80
sMyString = trim(Replace(Request.ServerVariables("QUERY_STRING"), "404;http://"&strDomain &":80", ""))

Dim strAryWords
Dim strValue
Dim myFSO
strValue = sMyString
strAryWords = Split(strValue, "/")
' - strAryWords is now an array
Dim i

For i = 0 to Ubound(strAryWords)
'Response.Write "<BR>"&i & " = " & strAryWords(i) & "<BR>"

'''HERE IS WHERE WE CAN USE SERVER.EXECUTE TO SET UP "FAKE" URL REWRITING:

'WE WILL ASSUME THAT IF "." IS IN THE ARRAY VALUE, THAT IT IS A FILE NAME NOT A FOLDER SO HERE WE FILTER FOR FOLDERS ONLY
if NOT instr(strAryWords(i),".") >0 then
strDir=strDir & strAryWords(i)& "/"
end if

if trim(Replace(strDir, "/", "")) = "404sub" then
if strExe =0 then ' strExeYes is for whether or not to show the error message inside div#1
Server.Execute "\404test\404sub\default1.asp" 'THIS IS AN EXAMPLE. COULD BE EXPANDED AS NEEDED WITH ARRAYS OR DATABASE CHECK OR WHATEVER
strExeYes=strExe
end if
strExe = strExe+1
end if

''' END SERVER.EXECUTE TO SET UP "FAKE" URL REWRITING

strFolder = preFolder & strAryWords(i) & "/"

'this line creates an instance of the File Scripting Object named myFSO
SET myFSO = Server.CreateObject("Scripting.FileSystemObject")
'if the folder does not exist
If NOT myFSO.FolderExists(strHomeFolder&strFolder) Then
if i = (Ubound(strAryWords)-1) then
if NOT MyString = strFolder &"/"& strFolder then
if NOT strExeYES =0 then

Response.write("<div id='layer1' style='position: absolute; width: 500px; z-index: 1; top: 50px'><p><font face=""Verdana"">I'm sorry, I can't find the page you requested:</font></p>" & vbNewline)
Response.write("<p><b><font face=""Verdana"">http://" &strDomain&sMyString &"</b><BR>" & vbNewline)
Response.write("<p><font face=""Verdana"">To return to my home page and site directory, click here:" & vbNewline)
Response.write("<a href=""http://ClassicASPreference.com/aspexamples/menu.asp"">" & vbNewline)
Response.write("http://ClassicASPreference.com/aspexamples/menu.asp</a> </font></p>" & vbNewline)
end if
end if
end if

preFolder = strAryWords(i)
fldr=MyString

Else


'START TEST FOR ALLOWED FOLDERS: PUT YOUR ALLOWED FOLDER NAMES INTO THE ARRAY AS SHOWN:

Dim fArray
fArray = Array("/aspexamples","/404test")
For Each item In fArray

'now we need to set up a Case with with to compare our current folder name to the allowed folder names in the array
if item = strFolder then
Cal = "YES"
end if
Next

If Cal = "YES" then
Response.write "<BR><font face='Verdana'>I did find this directory: <a href=http://" &(strDomain&""&strFolder)&">"&strDomain&""&strFolder&"</a></font>"
end if

End If
'this line destroys the instance of the File Scripting Object named myFSO
SET myFSO = NOTHING

Next

'THIS IS JUST TO CLOSE THE HTML
Response.write("</div></body>" & vbNewline)
Response.write("</html>" & vbNewline)

%>




 

OLD VERSION OF SCRIPT, USED WITH WINDOWS SERVER 2003

Copy and paste the code below into notepad. Replace the URL and other variables with your own. You will also need to replace the number 35 with the number of characters of your own Request.ServerVariables("QUERY_STRING") which may look something like 404;http://thequarterhorse.net/pageyoucantfind but minus everything after the third slash: 404;http://www.thequarterhorse.net/ - in other words, I counted the characters in 404;http://www.thequarterhorse.net/ to get the number 35.

<% OPTION EXPLICIT
response.status = "404 Not Found"


%>

<%
dim strF
strF = Request.ServerVariables("QUERY_STRING")
%>
<%
dim strFileName, str3, str3b, strExt,intHowLong,strFolder

strFileName = strF 'Getting the filename from our database and setting the string contents.
intHowLong = Len(strFileName) 'Getting the number of character contained within the filename.
if request.servervariables("SERVER_NAME")="www.YourSiteURLdotcom" then
replace strF, "404;http://www.", ""
str3b = intHowLong - 35
else
replace strF, "404;http://", ""
str3b = intHowLong - 31
end if
strFolder = Right(strFilename, str3b)
%><%

Dim myFSO
'this line creates an instance of the File Scripting Object named myFSO
SET myFSO = Server.CreateObject("Scripting.FileSystemObject")
'error handling here to make sure that things go smoothly
'if the folder does not exist
If NOT myFSO.FolderExists("D:\html\users\youraccountfolder\html\members\"&strFolder) Then
Response.write("<html>" & vbNewline)
Response.write("<head>" & vbNewline)
Response.write("<meta http-equiv=""Content-Language"" content=""en-us"">" & vbNewline)
Response.write("<meta http-equiv=""Content-Type"" content=""text/html; charset=windows-1252"">" & vbNewline)
Response.write("<title>The Quarter Horse Net page not found for quarter horses</title>" & vbNewline)
Response.write("</head>" & vbNewline)
Response.write("<body background=""http://yoursiteurl_com/images/qhdnetcanves.jpg"" leftmargin=""200"">" & vbNewline)
Response.write("<h2><font color=""#466530"" face=""Verdana"">Your Site Title</font></h2>" & vbNewline)
Response.write("<p><font face=""Verdana"">I'm sorry, I can't find the page you requested:</font></p>" & vbNewline)
Response.write("<p><b><font face=""Verdana"">" & strFolder &"</b><BR>" & vbNewline)
Response.write("<p><font face=""Verdana"">To return to my home page and site directory, click here:" & vbNewline)
Response.write("<a href=""http://www.YourSiteURL/default.asp"">" & vbNewline)
Response.write("www.YourSiteURL/default.asp</a> </font></p>" & vbNewline)
Response.write("</body>" & vbNewline)
Response.write("</html>" & vbNewline)
Else

Response.redirect("http://YourSiteURLdotcom/members/"&strFolder)&""
End If
'this line destroys the instance of the File Scripting Object named myFSO
SET myFSO = NOTHING

%>
 
ASP Examples Menu of Active Server Pages scripts code Classic ASP

Classic ASP Active Server Pages Examples, code, tutorials, scriptsASP Examples Menu (below*)



Active Server Pages | ASP | Scripts | Tutorials | Code | Web Programming | Examples
Active Server Pages ASP code examples, tutorials, and snippets for use in Programming interactive websites.

If you find omissions or errors let me know. I have moved these code samples to their own domain; if you find pieces missing, please let me know.
My favorite scripts site is CODANGO.

*

If any the information I've posted here has been useful to you, please donate.

Quick 'n  easy  dirty PHP script code wizard-generator

Zenphoto RSS XML feed used as data source for javascript slide show with PHP

Classic ASP to archive emails into database and save attached files to folder

PHP Image GD for thumbnails and image resizing with pathing for Windows servers

ODBC database connections for Microsoft Access MDB files with Windows 7: solution posted on this forum.

HTML5 embedded video player "Projekktor" completely skinnable with hyperlinked watermark

Edit web.config to allow Windows iis7 server to handle mp4 files or other mime types as needed

URL rewrite or make friendly URLs for iis7 Windows Server 2008 by editing your web.config file

FACEBOOK API GEEWHIZ - INTEGRATE FACEBOOK WITH YOUR WEBSITE

Use PHP to zip folders for backing up a website

Use PHP to archive emails in database and save attached files to folder

Use PHP with json to check how many hits left on Twitter API

PHP get single record from Access database without catchable fatal error message

PHP to make a bot to send automatic Twitter status updates with Abraham Williams' PHP Oauth Twitter Library

A way to use the PHP Oauth Twitter Library with your Classic ASP scripts for sending status updates

Get user's IP address location for your form results

Tutorial for registering an application with Twitter

Update your Twitter status with Classic ASP and Oauth seamlessly and invisibly without having to login to Twitter. Send status updates programmatically from a web page. PHP example too.

CLASSIC ASP: Cache your Twitter tweets in a database to keep your Twitter xml requests within the rate limit of 350 per hour / PHP VERSION

PHP: Use json to fetch Twitter followers' profiles

Index files content and insert into database for faster searches

Classic ASP with Ajax: secure URL check

Classic ASP with Ajax: referer check

Classic ASP with Ajax: a solution to the ampersand problem in querystrings

Very Simple Star Rating System for Classic ASP

Star Rating System uses CSS and JS adapated for Classic ASP from Yvo Schaap PHP script

XMLhttp with ASP to fetch and save photos from Photobucket account through RSS XML. Can be adapted for Youtube videos to import data to database.

NET image resize can be used to make thumbnails for Classic ASP

Track site visitors, get their ip address geographical data, count page views with Classic ASP

Javascript slideshow with Classic ASP to load image details dynamically from database photos table

Classic ASP with Ajax for rotating banners without page refresh and XML file converted to recordset as banner database
--Variation of the above but fetching a REMOTE XML file as the database
--Variation checks for secure URL and builds absolute URL and makes offsite banner links open in new window

True inline edit Classic ASP datagrid table database edit updating with Ajax SIMPLE EASY without page refresh!

Use Ajax with Classic ASP to preview data then update form with javascript url write

Yvo Schaap's easy inline edit Ajax: an example of using it to inline edit multiple rows of records in Classic ASP

Use Classic ASP to send text messages SMS from your website (Also shows how to use xmlhttp to post form data)

Ajax with Classic ASP for seamless, no page reload database record insertion

Nested SQL Statements to make temporary column names

Multiple records per row with paging

Display multiple records per row

Get ID of newly inserted record, Classic ASP, Access Database

Javascript timer with ASP to keep track of time expired until login expires / timeout session is up

Multiple Javascript event functions triggered from single form onsubmit

Get Querystring key names along with querystring variables

These are out of date but may still be of interest:

ASP Youtube Downloader  Version FOUR
All ASP code. Allows user client side downloads of Youtube videos. You have the option to allow files to be saved directly to your site also.
ASP/PHP video downloader (Works better than ASP for large files)

Use ASP to dynamically parse a Youtube XML playlist to render to html.

Zip up an archive file of your site for backup if you need to restore website with ASP and free Zip Component.

Zip all files in a folder into an archive or backup file dynamically by specific file extension.

Sanitize or clean price cost field for database insertion or display.

Javascript image dimensions and image file size preview.

Put recordset into array, assign keys to the records then use record associated with specific array key as a variable elsewhere.

Access Database Interface Generator Wizard to read your table and field names and to create a table display.

Combining dropdown selection box menu with dynamic ASP database page.

How to redirect user to original page they tried to view before logging, after they are logged in.

Previous/Current/Next Records, or Buddy Ring Script

Custom ASP 404 page not found error script: can also be used for URL rewriting

ASP Example: Combine ASP with Javascript for popup windows
dynamically sized to image dimensions.

ASP Example: How to use ASPJPEG to create thumbnails
Click for free Highly recommended thumbnailer for which you don't need Server access (doesn't seem to work on Windows Server 2008)

ASPjpeg: How to re-scale aspect ratio of thumbnails by height instead of width.

ASP Example: What if your host has an older version of ASPJPEG that doesn't support gifs?

Select Count Distinct Records for Access Database

Upload Image to folder and insert filename into database

Get date in such a way that it can be made part of a file name

Isolate a file name away from its path or extension or strip a path from a file name or strip characters from database field

User-customized on-the-fly stylesheet css files

Enhanced Page Hits Counter (also counts downloads and menu link hits)

Loop through array and compare to string with select case

Aphabetical Paging for your ASP scripts

Web Wiz Guide's Site Searcher Script modified as a file content indexer

Let Users Select How Many Records to View

Access Database for your PayPal IPN scripts

Miscellaneous collected code

From Classic ASP to PHP

You don't have to be a genius, just persistent, to write original Active Server Pages scripts

BuildApp - great old Classic ASP script generator!

Software Recommendation: bare_bones_no_bells_and_whistles_asp_code_generator_database_interface_maker (For PHP I recommend EasyGen.)

If any of this information is helpful to you, please consider donating with the button below. Thanks! ~~Lil