Forum OpenACS Development: ADP or TCL for 404 error page

Collapse
Posted by Dave Bauer on
Is it possible to use an ADP or Tcl page for the 404 error page with
OpenACS 4? I got it working, but it does not evaluate the adp.
Collapse
Posted by Bart Teeuwisse on

Dave,

redirects of error pages to ADP files is working for me with the following setup:

ns_section "ns/server/${server}/redirects"
    ns_param   401                 "/global/unauthorized.adp"
    ns_param   403                 "/global/forbidden.adp"
    ns_param   404                 "/global/file-not-found.adp"
    ns_param   503                 "/global/busy.adp"                
    ns_param   505                 "/global/error.adp"
Try this for /global/file-not-found.adp:
<html>
  <head>
  </head>
  <body>
    <%
    set hdrs [ns_conn headers]
    for {set i 0} {$i<[expr [ns_set size $hdrs] - 1]} {incr i} {    
       ns_puts "[ns_set key $hdrs $i] = [ns_set value $hdrs $i]<br>"   
    }
    %>
  </body>
</html>

And the request a non existing page after you have rebooted AOLServer.

Collapse
Posted by Rafael Calvo on
I have the same problem. After trying your code Bart, I get:
Connection = Keep-Alive
User-Agent = Mozilla/4.75 [en] (X11; U; Linux 2.2.16-22enterprise i686)
Host = mysite
Accept = image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*
Accept-Encoding = gzip
Accept-Language = en
Accept-Charset = iso-8859-1,*,utf-8
other ideas?
Collapse
Posted by Rafael Calvo on
just to restate the question. Can you use a templated adp file in the /global files?
Collapse
Posted by Neophytos Demetriou on
You can change ns_returnnotfound in acs-tcl/tcl/request-processor-procs.tcl to rp_server_abstract_file [acs_root_dir]/www/global/notfound.adp. Then, you'll have templating working.
Collapse
Posted by Neophytos Demetriou on
[typo above]: That should read rp_serve_abstract_file (not rp_server_abstract_file).
Collapse
Posted by Bart Teeuwisse on

My earlier example is only to demonstrate that redirecting to an ADP 404 error page does work. The response you see is the output of the example ADP page. Replace it with your own and more apropriate code. The Neophytos's solution has the big advantage of ATS templating.

The best solution in my opinion is to combine both suggestions. Keep the ns/server/${server}/redirects section in your config file and use a slightly modified redirect from Neophytos's original post like so:

rp_serve_abstract_file "[acs_root_dir]/www[ns_config ns/server/[ns_info server]/redirects 404 /global/file-not-found.adp]"

Your 404 error page can then be templated using the entry in the config file or if none is present the default [acs_root_dir]/www/global/file-not-found.adp. The other error pages are redirected to ADP pages that are not templated but can include calls to TCL procs and the database.

Maybe that it is also possible to intercept those redirects and serve abstract files?

Collapse
Posted by Stephen . on
The config file has been superceded by package parameters. The advantage of using ad_parameter is that the error pages can be changed on a subsite by subsite basis.
Collapse
Posted by Bart Teeuwisse on
Stephen, it is true that the config file has been superceded by package parameters but AOLServer is not aware of package parameters. For the other custom redirects like 403, 505, etc to work you have to use the config file.

You could make the 404 redirect a package parameter though so that it can be set on a subsite basis.

Collapse
Posted by Stephen . on
All requests are handled by the request processor so it has the oppertunity to respond to anything we might be interested in.  Other out of line, something-happened-send-back-a-message conditions, such as bad form input, aren't handled by the request processor or AOLserver's error mechanisms.  They should all be templated in a simillar way though.
Collapse
Posted by Denis Barut on
Here how have make it to work:

in the /packages/acs-tcl/request-processor-procs.tcl

proc rp_handler

....
  ad_call_proc_if_exists ds_add rp [list transformation [list notfound $root/$path notfound] $startclicks [clock clicks]]
  #ns_returnnotfound
  rp_serve_abstract_file [acs_root_dir]/www/global/notfound.adp

Collapse
Posted by Roberto Mello on
Wouldn't it be better if it was:

proc rp_handler

....

ad_call_proc_if_exists ds_add rp [list transformation [list notfound $root/$path notfound] $startclicks [clock clicks]]
  set servername [ns_info server]
  set config_value [ns_config "ns/server/$servername" NotFoundResponse]
  if ![empty_string_p $config_value] {
      rp_serve_abstract_file [acs_root_dir]/www${config_value}
      return
  } else {
      ns_returnnotfound
  }
...

That way the setting on the AOLserver config file would be respected.

Collapse
Posted by Denis Barut on
yes its much better !

Thank you

Collapse
Posted by Paul Doerwald on
I've done what's suggested in this thread, but all the pages are returning with HTTP code 200.  I'd like them to return with 404 or 500 so that Google knows what to do when it eventually comes around and indexes the site.

Is there an easy way to set the HTTP response code either by adding a flag for the rp_serve_abstract_file proc, or perhaps by setting the response code inside the templated ADP script?  Is there a way that a script can arbitrarily set a response code?  Something like ad_conn -set (though I don't think that ad_conn lets me do that)

Thanks,

Paul.

Collapse
Posted by Eduardo Pérez on
We would also like to return 404 when the file is not found, because it seems Google needs it for things like Google Sitemaps.

What would be the correct way to return a 404 error code by default when requesting a non existent page?

For example, if you request something like:
https://openacs.org/sadfasdfasfd
You'll get a strange page with comments from nowhere and a 200 OK error code.

Collapse
Posted by Brian Fenton on
You still get "a strange page with comments from nowhere and a 200 OK error code" when you go to https://openacs.org/sadfasdfasfd

Thanks for the tip Dave - I've always had problems getting those AOLserver error pages working (in fact I never have and they've never been enough of a priority - shame on me 😊 - hopefully will get it now).

Brian

Collapse
Posted by Dave Bauer on
I did this yesterday.

If you set an adp as the global redirect for file not found you can do this

&lt;% ns_return 404 text/plain "File not found" %&gt;

The same thing would work in a tcl file.

In the config file:

ns_section "ns/server/${server}/redirects"
ns_param 404 "/global/file-not-found.adp"

make sure the 404 page points to an ADP extension.

Collapse
Posted by Eduardo Pérez on
I'm asking because I'd like it to be a default in CVS so no one has to look at this page to have OpenACS deliver an standard behavior when "Page Not Found"

Would it be OK to commit these changes to CVS HEAD and oacs-5-2 ?

Collapse
Posted by Ryan Gallimore on
You can evaluate a templated adp file for 404s with the following. This will properly return a 404 error code.

In config.tcl:

ns_section ns/server/${server}/redirects
ns_param 404 "global/file-not-found.adp"

Create your templated adp page called 404.adp:

&lt;master&gt;

&lt;h2&gt;Sorry, that page cannot be found.&lt;/h2&gt;

In file-not-found.adp:

&lt;%
set rendered_page [ad_parse_template "/www/global/404"]
ns_return 404 text/html $rendered_page
%&gt;