Main Page
 The gatekeeper of reality is
 quantified imagination.

Stay notified when site changes by adding your email address:

Your Email:

Bookmark and Share
Email Notification
Project "ASP Redirect"
Purpose
The purpose of this project is to demonstrate how to redirect visitors going to a website to another website by rewriting the URL and preserving http/https and query string data.

How I Approach This Project
This code is based on your ability to replace code of an existing webpage so that it will redirect users (by rewriting the URL) to another website. The advantage of this approach is you can easily redirect users visiting a webpage to the same location on another website, and you just need a redirect setup on a few webpages. This disadvantage of this method is that every webpage in a website (you are redirecting from) would have to be setup either by copying and pasting the code into each ASP file, or by using ASP's "include" method (similar to PHP).

If you are looking for a redirect and URL rewrite for an entire website, you'll need to write your own ISAPI module for IIS 6/7 or locate one from the Internet. Otherwise, check out the code provided below; the only thing that you have to change is "www.another-website-domain-name.com" to point to the website domain name you wish to redirect users to.

<%
'
' Page Redirector/Rewriter.  Written by Joe McCormack.
'

' Domain Name to Redirect Visitor To
dim redirect_domain_name : redirect_domain_name = "www.another-website-domain-name.com"

' Do Not Use
dim protocol, domain_name, path, qstring, redirect_URL

' Get Protocol
if request.serverVariables("HTTPS") = "ON" Then : protocol = "https://" : Else : protocol = "http://" : End if

' Get Querystring
qstring = Request.QueryString
if Len(qstring) > 0 Then : qstring = "?" & qstring : End if

' Get Current Domain Name
domain_name = request.serverVariables("SERVER_NAME")

' Get Current Path
path = request.serverVariables("URL")

' Create New Path
redirect_URL = protocol & redirect_domain_name & path & qstring

' Redirect User To New Domain With Protocol and Path Intact
Response.Redirect(redirect_URL)
%>


ASP.NET 3.5
You can, naturally, do the same type of thing with ASP.NET as shown below. In the example below, if access is not in https, the URL is re-written to be https.

Dim browserConnectProtocol As String = Nothing
Dim browserDomain As String = Nothing
Dim browserPath As String = Nothing
browserConnectProtocol = Split(Request.URL.toString(), "://")(0)
browserDomain = Split(Split(Request.URL.toString(), "://")(1), "/")(0)
browserPath = Replace(Request.URL.toString(), browserConnectProtocol & "://" & browserDomain, "")
if LCase(browserConnectProtocol) = "http" Then
	if LCase(browserDomain) = "www.another-website-domain-name.com" Then
		Dim redirect_URL As String = Nothing
		redirect_URL = "https://" & browserDomain & browserPath
		Response.Redirect(redirect_URL)
	End if
End if
About Joe
Find Out Now!