Wrox Programmer Forums
|
Classic ASP Professional For advanced coder questions in ASP 3. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 12th, 2007, 12:50 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default Response.AddHeader Problem

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/...07/425963.aspx
http://www.webdeveloper.com/forum/ar...p/t-11443.html
http://forums.aspfree.com/asp-develo...ct-141800.html

And this is what I've come up with:
Code:
<%@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.

KWilliams
 
Old November 12th, 2007, 01:27 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

You could use a Meta Refresh instead:
<meta http-equiv="refresh" content="0;URL=<%=strNewURL%>" />

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
 
Old November 12th, 2007, 01:35 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I thought about using that, but if I did, the search engines wouldn't get the new URL's location without the 'Response.AddHeader("Location", strNewURL);' line.

If I do this:
<%
Response.Status = "301 Moved Permanently";
//Redirect to new page in 5 seconds
Response.AddHeader("Location", strNewURL);
%>
<html>
<head>
<meta http-equiv="refresh" content="5;URL="<%=strNewURL%> />
</head>
...the user gets redirected to the new page without a delay. If I remove the 'Response.AddHeader("Location", strNewURL);' line, then the search engines never update the URL in their database. Any other suggestions would be great.

KWilliams
 
Old November 12th, 2007, 01:55 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Well if, for example, you did something like this for every page on your site:

index.asp == index.aspx
myPage.asp == myPage.aspx

You could use the BeginRequest event of the global.asax and look for any pages going to the old asp page and then write code similar to:

Response.StatusCode = 301;
Response.AppendHeader("Location", newUrl);

Where newUrl is where you have manipulated the actual request.

Of course the user would never see your informational page and the redirect would happen automatically. It is a thought anyway.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
 
Old November 12th, 2007, 04:16 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Before trying a lot of workarounds, I messed around with the original code a bit to see where the problem lies. And when I comment out the 'Response.Status = "301 Moved Permanently";' line, the page redirects after 5 seconds in all browsers just fine. So I've been researching the "301 Moved Permanently" HTTP status code everywhere, and it appears that I've written it correctly. So I'm completely lost as to why this isn't working along with the 'Response.Status = "301 Moved Permanently";' status code. Any suggestions would be great. Thanks.

KWilliams
 
Old November 12th, 2007, 05:12 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Well here is the problem and, IMHO, I think that I.E. is handling this better the FF. The problem you are having is that ie refuses to display the website after you have issued a 301 to but, if you think about it, for any data to be wrote to the screen a status code of 200 has to be sent to the browser thus negating the 301.

IE bombs your page because you issue a 301 in the header and then try to write data to the screen. I think you will be stuck using the Location header for IE users and the Refresh header for FF users.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
 
Old November 13th, 2007, 06:26 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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:
Code:
<%@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.

KWilliams





Similar Threads
Thread Thread Starter Forum Replies Last Post
HttpResponse response problem??? thomaz C# 1 February 22nd, 2006 08:28 PM
Problem with response.sendredirect urbuddie JSP Basics 0 September 15th, 2005 02:38 PM
Page getting freezed after Response.AddHeader spacy ASP.NET 1.x and 2.0 Application Design 0 October 6th, 2004 06:51 AM
Problem with Response.ContentType nagesh_vasa XML 1 May 23rd, 2004 01:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.