Jump to content

Response.AddHeader Problem


kwilliams

Recommended Posts

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 moved2) Redirect the user to the new URL after a 5 second delay, so that they can see a note about the changeI've found a few articles on how to properly do this at:http://blogs.msdn.com/samar/archive/2005/06/07/425963.aspxhttp://www.webdeveloper.com/forum/archive/...hp/t-11443.htmlhttp://forums.aspfree.com/asp-development-...ect-141800.htmlAnd 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 variablesvar strOldURL = "oldpage.asp";var strNewURL = "newpage.aspx"//Update search enginesResponse.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.

Link to comment
Share on other sites

Try using "Refresh" instead of "REFRESH". The Refresh header was introduced by Netscape and Microsoft, so IE definately supports it. You can get a list of HTTP status codes here:http://en.wikipedia.org/wiki/List_of_HTTP_status_codesYou can also use a meta tag to do the refresh instead of sending a header.

Link to comment
Share on other sites

Try using "Refresh" instead of "REFRESH". The Refresh header was introduced by Netscape and Microsoft, so IE definately supports it. You can get a list of HTTP status codes here:http://en.wikipedia.org/wiki/List_of_HTTP_status_codesYou can also use a meta tag to do the refresh instead of sending a header.
I changed "REFRESH" to "Refresh", but it had no affect. And Unfortunately I can't use the meta tag refresh method instead of this method, because my goal is to first inform the search engines that the link has changed before redirecting the user.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.
Link to comment
Share on other sites

Have you tried leaving out the redirect? Because according to this, the browser is supposed to do the redirect itself when it gets a 301 response:

The 301 response from your Web server should always include an alternative URL to which redirection should occur. If it does, a Web browser will immediately retry the alternative URL.
Link to comment
Share on other sites

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 variablesvar strOldURL = "oldpage.asp";var strNewURL = "newpage.aspx"%><html><head><title>Auto-Redirect</title><%var todaysDate = new Date();//current datevar oldSeconds = todaysDate.getSeconds();//current secondsvar 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 integerif (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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...