You can do this with a ns_register_url, like this:
ns_register_proc GET /* Redirect
proc Redirect { conn spec } {
set host [ns_set get [ ns_conn headers ] Host ]
set url [lindex [ns_conn request 1]]
if { $host != "$correcthost" } {
ns_returnredirect http://$correcthost/$url
}
}
I just whipped this up on the spot without proper testing, so try it
out and see, it should get you started.
Note that I only did this for GET. I don't know what would happen if you
redirected a POST, and besides, you have control over where a POST goes
anyway, usually (because you made the form)
Also, I made my own procedure, which I use, in place of ns_returnredirect,
which at least tries to always redirect the user to the sameg host he came
in on:
proc dummy_returnredirect { href } {
set return $href
if ![string equal -length 4 $href "http"] {
set host [ns_set get [ ns_conn headers ] Host ]
set return "[ string trim ${host}/$href ]"
regsub -all {//} $return {/} return
set return http://$return
}
ns_returnredirect "$return"
}
Note that:
* if you give an absolute URL, it does not attempt to mangle the host
* if you do: "dummy_returnredirect mypage"
it goes to /mypage, and is not relative to wherever you currently are
It's crude, but it works. The idea is, instead of having a somewhat
clumsy registered proc you have to go through for every hit to your site,
just make the redirect do the Right Thing in the first place.
Rusty
On Tue, 11 Jun 2002, manish mukherjee wrote:
> hi all,
>
> i'm using cookies to manage user authentication. i also use
> "ns_returnredirect /" at various points in the site to return the user to
> the home page. since i've specified the hostname as "mysite.com" in the
> nsd.tcl file, they are returned to "mysite.com" even if they started out
> at "www.mysite.com/someurl".
>
> so the problem is that if a user logs in at "www.mysite.com" instead of
> "mysite.com", and then at some point they are redirected to the home page
> at "mysite.com", the server makes them log in again.
>
> i've been trying to figure out how to redirect all requests for
> "www.mysite.com" to "mysite.com" and the only possible solution seems to
> be nsvhr. am is missing something?
>
> i'm running aolserver 3.4.2 and it seems that nsvhr only works with
> version 3.3, so i'm kind of at a loss as to what i should do.
>
> thanks.
>
> mkm
>
------------------------------------------
Rusty Brooks : http://www.rustybrooks.org/
------------------------------------------