Applescript CGI Redirects

Rather than generating a complete HTTP response de novo, it might be simpler to send your visitor to a pre-existing page, perhaps a confirmation of a form submission, or a standard error page. This is easy, once you know how.

Basic redirect example
property crlf : (ASCII character 13) & (ASCII character 10) property redir_header : "HTTP/1.0 301 REDIRECT" & crlf & "Server: WebSTAR 4.4" & crlf & "Location: " property redirPage: "http://www.linkedresources.com/teach/applescript/writingCGIs.shtml" on handle CGI request set newhtml to redir_header & redirPage & crlf & crlf -- both "crlf"s needed! return newhtml end handle CGI request

You would of course want to wrap your own logic around it, including form processing, error handling, possibly a different redirect depending on input.

You can copy the above code from this text field

The following are all valid assignments for redirPage:
property redirPage: "http://www.remotesite.com/folder/directory/file.html" property redirPage: "file_in_same_folder.html" property redirpage: "folder/file_in_subfolder.html" property redirpage: "/path/from/site_root.html" property redirPage: "../parent_folder/file.html"

Naturally, redirPage can be set inside your handler logic, and can be dynamically generated. It can even be extracted from your form_post data.

One of the interesting and powerful capabilities redirects allow is to build a URL with a search string in the GET format:

set searchstring to "?user=jeff&passwd=secret&params=whatever&action=execute" set newhtml to redir_header & redirPage & searchstring & crlf & crlf

This allows for dynamic redirects to pages which use Lasso, PHP, Javascript, or whatever. For example, your redirect to a confirmation page could include a Javascript which thanks the user by name.

Well, that's all there is to redirects (that I know of and use, anyway). If you have found this page to be helpful, please send me an email to encourage me to continue this teaching series.