Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirects - META or server side?

Status
Not open for further replies.

LEICJDN1

Technical User
Nov 27, 2002
201
0
0
GB
I originally wrote my site as a whole bunch of html pages. I have now upgraded to ASP hosting, and have written new sections, but now wish to switch the html pages to asp pages to make use of server side includes and other features.

I guess people will have bookmarked the index page, and also some of the other main 'chapter' pages. Index will remain as .html, but I have switched the other pages to .asp

In order to ensure those with .html bookmarked end up as .asp i need to redirect them.

Started with a simple Meta refresh command which works fine, but is there a better and more compliant way?

Have heard of server side redirects but am unsure how to use these from an .html page to redirect to an .asp page of the same name.

Thanks.
 
If you have an apache and access to the .htaccess file, it's actually pretty easy to do. I don't know how it works in IIS though, and most probably, if you have ASP, you'll use an IIS...

With .htaccess, you could just add these lines:

Code:
rewriteengine on
RewriteBase /
RewriteRule ^([^.]*).html $1.asp [L]

(I think so at least, haven't checked and I'm not that used to using RegExp - but it would be something along these lines)

haslo@haslo.ch - www.haslo.ch​
 
You can do something like this (albeit this is for a few pages)
Script courtesty of 4guys from rolla (I think)

Code:
<%
'check the URL used to get here and redirect if it is a common/renamed page, else show the custom 404 and alert webmaster
SomeDir = Request.ServerVariables("Query_String")
SomeDir = UCase(Mid(SomeDir, InStrRev(SomeDir, "/") + 1, Len(SomeDir)))
'SomeDir now contains whatever the user typed after the last
'slash in the URL, if he typed [URL unfurl="true"]http://www.foo.com/books[/URL] SomeDir 
'will contain "BOOKS".
'the url is converted to caps and checked as caps to make sure that what the user has typed and what we check for are the same case. i.e. ALWAYS all caps.

 Select Case SomeDir
	Case "MEDIAINFO.ASP"
		Response.Redirect("newsArchive.asp")
	Case "PRN-AWARDS.ASP"
		Response.Redirect("awards.asp")
	Case "DEFAULT-BEAR.ASP"	
		Response.Redirect("index.asp")
	Case "DEFAULT.ASP"	
		Response.Redirect("index.asp")
  Case Else

%>

[i]some HTML for your custom 404 page goes here.[/i]

If you have alot of pages, you might want to set up an array containing the redirects.
Or if your pages have the same names but a different extension you might just want to use some string functions to convert the name of the requested page.

You will need to set up IIS to point any 404's to this script and create HTML for the 404 page for requests that don't match a new page.

 
That code works, if you put it in an ASP page. It doesn't work in a HTML page though, that will just send the plain text to the browser.

What you could try as well would be to let the server interpret HTML pages as well as ASP pages, just treat every HTML page as an ASP. That should work, must be somewhere in the IIS configuration. Like this, you wouldn't even have to switch to new URLs (as long as the site structure is the same)...

haslo@haslo.ch - www.haslo.ch​
 
The code works with an ASP page and is meant to be implemented as the 404 error document.

Remove all the HTML files from the server, when the requests generates a 404 error the server will (if configured) send the 404 document which will, in turn, redirect to the correct page.

Additionally, you could use 301 type redirects to indicate that the URL has moved permenantly.

Configuring the server to run every page as an asp page is wasteful of resources if ever an HTML page was added to the site. Though it will suit this instance where every page is an asp page.

 
Thanks all, plenty there for me to play and learn with.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top