This example can be found in the examples/tcl/redirect.tcl
file.
# Example 4: Implementing redirects with ns_respond and # ns_write # # /example/not_here uses ns_respond to return an HTTP # redirect to /example/finaldest. # /example/not_here2 does the same thing using ns_write # /example/not_here3 does the same thing with # ns_returnredirect # # Things to notice: # # * When you use ns_write, you need to compose the # entire response. # # * "ns_conn location" returns the http://hostname # part of the URL that you can use to generate # fully qualified URLs. # # * ns_returnredirect is a lot simpler than either # ns_respond or ns_write. ns_register_proc GET /example/finaldest finaldest ns_register_proc GET /example/not_here not_here ns_register_proc GET /example/not_here2 not_here2 ns_register_proc GET /example/not_here3 not_here3 proc not_here {conn ignore} { set headers [ns_set new myheaders] ns_set put $headers Location \ [ns_conn location $conn]/example/finaldest ns_respond $conn -status 302 -type text/plain \ -string "Redirection" -headers $headers } proc not_here2 {conn context} { set content \ "<HTML><HEAD><TITLE>Redirection</TITLE></HEAD><BODY> <H1>Redirection</H1>The actual location of what you were looking for is <A HREF=\"[ns_conn location $conn]/example/finaldest\"> here.</A> </BODY></HTML>" ns_write $conn \ "HTTP/1.0 302 Document follows MIME-Version: 1.0 Content-Type: text/html Content-Length: [string length $content] Location: [ns_conn location $conn]/example/finaldest $content" } proc finaldest {conn context} { ns_return $conn 200 text/plain \ "You have arrived at the final destination." } proc not_here3 {conn context} { ns_returnredirect $conn \ [ns_conn location $conn]/example/finaldest }