Forum OpenACS Q&A: Re: Best way to do HTTP 3xx Redirections?

Collapse
Posted by Michael Bluett on
My version of Joel's code redirects the user to the missing URL without an extension if a .vuh file exists (e.g. /emails from /emails.htm). This may activate the .vuh file (e.g. "/emails.vuh") or another file if a more appropriate one exists (e.g. an emails.tcl/emails.adp pair).
It should also deal with directory names containing periods.
    set url [ad_conn url]
    # Find the end of the URL, that which is after the last forward slash
    set url_tail [string range $url [expr [string last / $url] + 1] end]
    # ns_log "Notice" "url_tail:$url_tail"
    if {[string first . $url_tail] > 0} {
	# Lets try chopping off the extension and see if there is a suitable
	# file
	set path_and_extension [string range $path 0 [expr [string last . $path] - 1] ]
	append path_and_extension $extension_pattern
	# ns_log "Notice" "path_and_extension:$path_and_extension"
	if { [file isfile $path_and_extension] } {
	    set no_extension [string range $url 0 [expr [string last . $url] - 1] ]
	    # ns_log "Notice" "no_extension:$no_extension"
	    ad_raise redirect $no_extension
	}
    }
-The $extension_pattern is provided when the procedure is called, initially it is .* (which doesn't find the .vuh file) and then .vuh. One could hardcode other items in here to be searched for that would also be activated, such as .html.
Collapse
Posted by Michael Bluett on
I have more information to add to this :- Google likes 301 redirects (permanent redirects), so I have looked up how to do 301 redirects (ad_returnredirect simply returns a 302 redirect). The aolserver manual suggests this:
Using ns_respond, it's easy to do an HTTP redirect: 
set headers [ns_set new myheaders]
ns_set put $headers location http://www.aolserver.com
ns_respond -status 302 -type text/plain \
    -string "redirection" -headers $headers
By changing the 302 to 301 this should work fine. This is primarily to replace existing Apache "RedirectPermanent/Redirect permanent" directives.
Collapse
Posted by Andrew Piskorski on
Hm, this 301 redirect sounds like a good feature to have. ns_returnredirect (which is what ad_returnredirect calls) always does a 302 though. Sounds like a good idea to either add a switch to let it optionally do a 301 instead, or add a new command, ns_returnmoved or ns_returnredirectpermanent or something like that. Should be pretty easy to change the AOLserver Ns_ConnReturnRedirect C function to make it happen.

So, I just filed a feature request for that on SourceForge

Of course, without an extension to the AOLserver API like that, you should be able to always just do it with a Tcl proc like you said.