On Mon, Nov 12, 2012 at 9:09 AM, Ian <yaws@poetmail.net> wrote:
Thanks for the replies. I only discovered it by accident - I assumed
that the default would be 301. Don't you think this should be the
default? I know that I rarely need a 302.

Maybe 301 vs 302 could simply be implemented by using something like ":"
& "::" vs "=" & "==" (just a thought).


We'll probably extend the current syntax of the redirect block to allow an optional status code to precede the redirect URL.

 
Most of my sites are driven by a custom 404 handler so I would actually
use this for pages instead of any native server method. My main concern
is redirecting domain.tld to www.domain.tld.

I can easily implement an arg_rewrite in the server config for all the
non www domains but I'm new to YAWS/Erlang and I don't know how to do
this. I can't even find a function reference (Googling for
"url_decode_q_split" for example gets me nowhere).

Logically, this is what is needed:

Get the request
Extract the http protocol
Extract the http host
if(string:to_lower(string:sub_string(host,1,3)) != "www")
{
  send a 301 header
  redirect to protocol+"www"+host
}

Any help would be greatly appreciated.


I might be missing something but I think if you put the following in a file named redir.erl, compile it, put the resulting beam file on the load path, and register the redir module in your server block in yaws.conf as an appmod on path "/", it should do what you want.

-module(redir).
-export([out/1]).

-include_lib("yaws/include/yaws_api.hrl").

out(Arg) ->
    Url = yaws_api:request_url(Arg),
    case Url#url.host of
        "www."++_ ->
            {page, Url#url.path};
        Host ->
            NewUrl = yaws_api:format_url(Url#url{host="www."++Host}),
            {redirect, NewUrl, 301}
    end.


--steve