Click to See Complete Forum and Search --> : Response.AddHeader Problem


kwilliams
11-12-2007, 11:50 AM
I have an old ASP site, and I've created a new ASP.NET site. I'm trying to set up an auto-redirect on each of the old ASP pages that will:

1) Tell the search engines that the URL has permanently moved
2) Redirect the user to the new URL after a 5 second delay, so that they can see a note about the change

I've found a few articles on how to properly do this at:
http://blogs.msdn.com/samar/archive/2005/06/07/425963.aspx
http://www.webdeveloper.com/forum/archive/index.php/t-11443.html
http://forums.aspfree.com/asp-development-5/delay-response-redirect-141800.html

And this is what I've come up with:
<%@LANGUAGE="JAVASCRIPT" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
Response.Buffer = true;

//Declare variables
var strOldURL = "oldpage.asp";
var strNewURL = "newpage.aspx"

//Update search engines
Response.Status = "301 Permanently Moved";
//Redirect to new page in 5 seconds
//Response.AddHeader("REFRESH", "5;URL=" + strNewURL);//NOT WORKING IN IE - LIVE
%>
<html>
<head></head>
<body>
<h2>The Page Cannot Be Found</h2>
<br />
The page you are looking for has moved.
<br />
<br />
<strong>Old Location: </strong><%=(strOldURL)%>
<br />
<strong>New Location: </strong><a href="<%=(strNewURL)%>"><%=(strNewURL)%></a>
<br />
<br />
You will be redirected to the new page in 5 seconds. Please correct any broken bookmarks.
</body>
</html>
This solution works fine in Firefox, but not in IE6 or IE7 (The page cannot be displayed). One of the articles noted that the "Response.AddHeader" is controlled by the browser, so it won't alway work. But I don't know of another way to create a delay within an ASP page using JavaScript syntax. Also, I've seen "301 Moved Permanently" and "301 Permanently Moved" used in several examples, but I don't see which of these is proper...or will they both work fine? If anyone can let me know what I'm doing wrong, that would be great. Thanks.

Aengus
11-12-2007, 09:02 PM
In line 1 you have: <%@LANGUAGE="JAVASCRIPT" %>

Isn't it supposed to be: <%@LANGUAGE="VBSCRIPT" %>

kwilliams
11-13-2007, 09:12 AM
In line 1 you have: <%@LANGUAGE="JAVASCRIPT" %>
Isn't it supposed to be: <%@LANGUAGE="VBSCRIPT" %>

I'm using JavaScript syntax instead of VBScript syntax on this page, so the way it's posted is correct.

kwilliams
11-13-2007, 05:27 PM
Well, after some additional scripting, I've come up with a workaround. It pulls the seconds from the user's clock, and then redirects them in 5-10 seconds, depending if they have IE or FF. Here's the code, in-case anyone's interested:

<%@LANGUAGE="JAVASCRIPT" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
Response.Buffer = true;
//Declare variables
var strOldURL = "oldpage.asp";
var strNewURL = "newpage.aspx"
%>
<html>
<head>
<title>Auto-Redirect</title>
<%
var todaysDate = new Date();//current date
var oldSeconds = todaysDate.getSeconds();//current seconds
var refreshURL = strOldURL + "?counter=" + oldSeconds;//querystring
%>
<meta http-equiv="refresh" content="5;URL=<%=(refreshURL)%>" />
<%
var counterCheck = Request.QueryString("counter");
var counterCheckAdd = parseInt(counterCheck);//convert string to integer

if (oldSeconds == (counterCheckAdd + 5)) {//then
Response.Status = "301 Moved Permanently";//Update search engines
Response.AddHeader("Location", strNewURL);//Redirect user to new URL
}//end if
%>
</head>
<body>
<h2>The Page Has Moved</h2>
The page you are looking for has moved.
<br />
<br />
<strong>Old Location: </strong><%=(strOldURL)%>
<br />
<strong>New Location: </strong><a href="<%=(strNewURL)%>"><%=(strNewURL)%></a>
<br />
<br />
You will be automatically redirected to the new page in just a moment. Please update your bookmarks and/or links.
</body>
</html>

It can take 5-10 seconds, bit it works well for me, so that's a good thing. Thanks so much for your help.