Improving URL structure is important for SEO and user experience. On a company website, the original links looked like this:
http://www.mywebsite.com/default.asp?midframe=/WebApp/Schedules.htm
These URLs are long, unclear and not SEO friendly. The goal was to change them to clean, readable URLs like:
http://www.mywebsite.com/DailySchedule/
This article explains how to implement such URL rewriting in classic ASP using IISβs custom 404 error page handling.
π§ Step 1: Change IIS 404 Error Page to a Custom ASP File
Since clicking on the new clean URL initially caused a "Page Not Found" error, the first step was to configure IIS to use a custom 404 error page instead of the default.
In IIS settings, replace the 404 error page with an ASP file (e.g.,
404handler.asp
).This file will capture the requested URL and perform the URL rewriting.
π΅οΈ Step 2: Capture the Requested URL Using Server Variables
Inside the custom ASP file, use the server variable Request.ServerVariables("QUERY_STRING")
to get the original requested URL.
Example content of this variable might look like:
404;http://www.mywebsite.com:80/DailySchedule
π οΈ Step 3: Extract the Relevant Part of the URL
Use the Split
function in ASP to break the URL string into parts based on the /
delimiter.
TheURL2 = Split(TheURL, "/")
π Step 4: Lookup URL Mapping in a Database
Use an Access database to store mappings between friendly URLs and actual page paths.
The database table Pages
contains two columns:
p_Name | p_URL |
---|---|
Academics | /WebApp/Academics.htm |
AdmissionHome | /WebApp/AdmissionHome.asp |
DailySchedule | /WebApp/DailySchedule.htm |
ποΈ Step 5: Match the URL Segment to a Database Record
Use an SQL query to find the matching page path:
rst.open "select * from Pages where p_Name='" & TheURL2(3) & "'"
If a matching record exists, proceed to load the content.
If not, display a "Page Not Found" message (or the real 404 page).
π₯οΈ Step 6: Render the Page Using Server.Execute
If a match is found, use the following flow:
call HEADER() ' Outputs page header and left menu
server.Execute(rst("p_URL")) ' Inserts the requested content page
call FOOTER() ' Outputs page footer and right menu
HEADER()
andFOOTER()
are procedures saved in a file nameddesign.asp
.They handle the common page layout parts like header, menus and footer.
The content from the database path is loaded in the middle dynamically.
β Summary and Benefits
This URL rewriting approach uses:
IIS 404 error handling to capture requests for nonexistent pages
ASP to parse and map clean URLs to actual ASP or HTML files
A database for flexible URL-to-page mapping
Modular page rendering with common layout parts for maintainability
This method improves SEO by offering clean URLs that are easier to read and share. It also helps maintain site navigation consistency and reduces dependency on query strings.