A Conversation for Website Developer's Forum
cunning re-direct script required
C Hawke Started conversation Sep 2, 2003
Couldn't see anything after a quick web search to do this - I know i can be done, and probably could do it if spent the rest of the week, but.....
...after implementing a dataabse metatag system on a site, (all tags are stored in Access database and retrieved on page, therefore allowing site index and stuff much easier) all my pages have changed from .htm to .asp
So I need a 404 page that contains script to remove the extension of the missing page, add the new extension then direct people to that one
AND
Be clever enough to realise when a real missing page has been entered (ie one never that never had the .htm or never existed) and not go into a loop
Anyone?
The other option is placing a redirect on ALL the 300 odd html pages to the new .asp page.
CHawke
cunning re-direct script required
Bogie Posted Sep 2, 2003
The only way I can think of to do this is to have direct access to the IIS Console on the web-server... do you have physical access to the machine your site is hosted on?
B.
cunning re-direct script required
Bogie Posted Sep 2, 2003
Log onto the machine using the "Local Administrator" account.
Open the IIS Control Console at "START" - "Settings" - "Control Panel" - "Administrative Tools" and open "Internet Services Manager".
Expand the left tree pane menu to show computer name and services.
Find your service in the left hand tree pane, right click on the web service, and choose "Properties" from the drop down menu. A panel will open up called "[Description] Properties" with nine tabs.
Open the tab "Custom Errors"... it allows error messages to be sent to the user in the form of a web page which can be specified. The following URL passes on the error message codes to an ASP web page. The values need to be set as follows:
To alter a value, highlight an error code (the main one is 404 - "Page not found") in the box, and click the "Edit Properties" button. A panel will appear. Change the "Message type" to "URL" (If it is available), then enter the address "/errors/default.asp?number=NUMBER&address=" in the box "URL". Replace the word "NUMBER" with the error message number... for example, if you were setting error message 404, you would enter the URL "/errors/default.asp?number=404&address=".
Click the "OK" button to close the panel. Now exit the "Internet Services Manager" console.
Open a web page editor and open the ROOT folder level of your site... create a folder called "errors" and put a new ASP page called "default.asp" in it. The code for that page is:
<%@ LANGUAGE="VBScript" %>
<% Option Explicit %>
<%
Dim Address
Dim Number
Address = request.querystring ("address")
Number = request.querystring ("number")
Response.redirect (Replace(Address),".htm",".asp")
%>
That should work.
B.
cunning re-direct script required
Bogie Posted Sep 2, 2003
Side note... some web-site hosting companies are really nice and will set the custon error message page in the IIS console (see previous messge) with your URL of choice (like "/errors/default.asp?number=404&address=") for you... so all you have to do is create the redirect file as suggested.
B.
cunning re-direct script required
C Hawke Posted Sep 2, 2003
Bogie
That last bit of code is all I needed thanks - our hosts are very good and already have set up a custom page for us, will try the routine tomorrow at work
Many thanks
CHawke
cunning re-direct script required
DoctorMO (Keeper of the Computer, Guru, Community Artist) Posted Sep 2, 2003
I'd have done it in perl, no scratch that I would have done the whole website in perl,
It saddens me some what the number of crappy asp questions.
- DoctorMO --
cunning re-direct script required
C Hawke Posted Sep 3, 2003
it saddens me at programming snobery
ASP is what I know - evolved naturally from xBase, to VBA, now to VBscript - if I was to start again who knows, but database programming is where I came from and alas is what I know.
If you can't help, don't post.
CHawke
cunning re-direct script required
Ion the Naysayer Posted Sep 3, 2003
Something you may want to look into for the future - in most web server software (including IIS, as far as I'm aware) there's a way to configure the server so that when it gets a request like:
GET /foo HTTP/1.1
it will serve up the correct version (say, foo.bar). The W3C website (http://www.w3.org/) does this, particularly with images - it will send a PNG if the browser advertises that it supports PNG, or it will send a JPEG if it doesn't. I'm not sure exactly how to do it under IIS but I'm fairly certain that it's possible.
cunning re-direct script required
C Hawke Posted Sep 8, 2003
in the end I had to bodge it a bit - any suggestions to tidy it -
First page is as follows
Dim Address
Dim Number
dim counter
session("counter")=1
Address = request.querystring ("address")
Number = request.querystring ("number")
address= Replace(Address,".htm",".asp")
response.Redirect("/error3.asp?address=" & address)
then error3 is -
<%
Dim Address
Dim Number
if Session("counter")=1 then
Address = request.querystring ("address")
Number = request.querystring ("number")
address= Replace(Address,".htm",".asp")
address=replace(address,"?404;","")
Response.Redirect(address)
else
session("counter")=0
response.redirect("/error2.asp")
end if
%>
Basically, the code as so kindly provided above was OK for pages I had changed from htm to asp, however for genuine 404 errors - badly typed urls, guesses etc, the routine looped out and returned the default page. So error2.asp is the standard "sorry but this page doesn't exixt" page.
Thanks for the pointers.
CHawke
cunning re-direct script required
C Hawke Posted Sep 8, 2003
DOH!
Got that wrong.
Not sure what's going on now, cut and pasting erros, but not as easy as I thouht.
CHawke
cunning re-direct script required
Bogie Posted Sep 8, 2003
Try this on /error/default.asp
<%@ LANGUAGE="VBScript" %>
<% Option Explicit %>
<%
Dim Address
Dim Number
Address = request.querystring ("address")
Number = request.querystring ("number")
' Run checks:
IF Number <> "404" THEN
' This is NOT a page not found error:
Response.redirect ("/error/error-message.asp?number=" & Number )
ELSE
' This IS a page not found error, so run an additional check:
IF Right(Address,4) = ".asp" THEN
' The address already had .asp at the end, so this is a page not found error:
Response.redirect ("/error/error-message.asp?number=404")
ELSE
' The address had .htm at the end, so this is a simple file extension swap:
Response.redirect (Replace(Address),".htm",".asp")
END IF
END IF
%>
cunning re-direct script required
C Hawke Posted Sep 8, 2003
cheers, sort of sorted the bodge page, with a extra session variable, but when I have a chance I'll see if I can get it to a single page, no session variable solution, whilst our hosts are good it does take time to make changes and the redirect isn't quite perfect on the server yet - not sending the number.
But I have something that does work now, so thanks a huge amount, just deleted about 100 htm pages in the knowledge that the information can be found.
CHawke
Key: Complain about this post
cunning re-direct script required
- 1: C Hawke (Sep 2, 2003)
- 2: Bogie (Sep 2, 2003)
- 3: Bogie (Sep 2, 2003)
- 4: Bogie (Sep 2, 2003)
- 5: C Hawke (Sep 2, 2003)
- 6: DoctorMO (Keeper of the Computer, Guru, Community Artist) (Sep 2, 2003)
- 7: C Hawke (Sep 3, 2003)
- 8: Ion the Naysayer (Sep 3, 2003)
- 9: C Hawke (Sep 8, 2003)
- 10: C Hawke (Sep 8, 2003)
- 11: Bogie (Sep 8, 2003)
- 12: C Hawke (Sep 8, 2003)
- 13: Bogie (Sep 9, 2003)
More Conversations for Website Developer's Forum
Write an Entry
"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."