Using WebConnect 5.x, I'm attempting to return a 404 error and a 'not found' page when an invalid or disused link is used:
oHeader = CREATE("wwHTTPHeader",Response)
oHeader.setprotocol([HTTP/1.1 404 Not Found])
oHeader.CompleteHeader()
Response.WriteLn([<html><meta http-equiv="refresh"content="0;url=/404b.asp"></html>])
This seems to work fine, except that when Google or someone hits that link, it returns a 200 OK code rather than a 404 Not Found code. Here is what Google sees:
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html
Content-Length: 118
Vary: Accept-Encoding
Server: Microsoft-IIS/8.0
RequestId: 133_12f4d692
Set-Cookie: InterlightSession=55C0VNES910884; path=/
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 07 Mar 2018 20:46:00 GMT
HTTP/1.1 404 Not Found
RequestId: 133_12f4d692
<html><meta http-equiv="refresh"content="0;url=/404b.asp"></html>
So I'm trying to figure out how to return a 404 error at the top of the returned results so Google knows it can remove it from my site index.
Don't use wwHttpHeader - that's old syntax and although that might still work it's way more verbose than it should be.
You also need to clear your response first before you send the updated 404.
This should work:
Response.Clear()
Response.Status = "404 Not Found"
RETURN
I use this all the time especially in API backends.
+++ Rick ---
That will just return the standard IIS 404 message. I'd like to return a custom page. Can't seem to find the right IIS 10 settings to set it through IIS so I was hoping to use webconnect's expandtemplate().
Hmmm... I think you're using the ISAPI module rather than the .NET Module?
There are IIS and ASP.NET settings that control how error messages are returned. Web Connection's default setup with the .NET Module will serve errors as content. I had to double check but if I simply add:
Response.Status = "404 Not Found"
to the bottom of a request the original page will still display even though the response returns a 404
header.
The web.config
setting in WWWC ships with:
<configuration>
<system.webServer>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
</configuration>
The .NET Module also calls Response.TrySkipIisCustomErrors
to force errors to just display - there's no equivalent for ISAPI, but it should respond to the above switch.
Here's a lot more info on how IIS error messages are handled in an older blog post:
+++ Rick ---