Redirects

Redirs are a powerful tool in the webapp programmer toolbox. The Webserver returns a specific status code (302) and adds a "Location:" header to the responce headers to the Browser. The Browser then displays the new page as indicated in the "Location" header.

Yaws supports a number of different forms of redirect return values from the out/1 function.

The code:


%% redirect2.yaws

out(_Arg) ->
    L="https://www.google.com/search?num=20&hl=en&lr=lang_en%7Clang_sv&q=yaws",
    {redirect, L}.

Clickable On this link executes the above redirect code.

The code above redirects to an external URL. The HTTP RFC mandates that the Loction header must contain complete URLs, including the the method, http, https etc. A very common case of redirection, is a to redirect to another file on the same server. The code in redirect3.yaws shows an example of a yaws redirect relative to the "current" server.


%% redirect 3 yaws

out(_Arg) ->
    {redirect_local, "/redirect2.yaws"}.

The code in redirect3.yaws will do a relative redirect to the code in redirect2.yaws which in its turn redirects, once again, to google. Double redirects.

While working with redirects, the tool curl is an excellent way to troubleshoot the behaviour of your redirects. For example:


    # curl -I http://rubin.hyber.org:8000/redirect3.yaws

    HTTP/1.1 302 Found
    Server: Yaws/1.49 Yet Another Web Server
    Location: http://rubin.hyber.org:8000/redirect2.yaws
    Date: Tue, 16 Nov 2004 20:16:01 GMT
    Content-Type: text/html

  

Where http://rubin.hyber.org:8000 is where I am currently testing the redirect3.yaws code. Learn and use the curl web client, it may not render pictures pretty, but it sure displays headers.


We show one additional version of redirect code. The code in redirect3.yaws requires an absolute path. If we want to supply a path relative to the current url, we can use either the Redirect modifier rel_path or any_path as in :


%% redirect4.yaws

out(_Arg) ->
    {redirect_local, {any_path, "redirect2.yaws"}}.

Clickable here

You can also redirect a whole site via virtual server configuration. For example, you can redirect all port 80 requests to port 443 like this:

    <server vhost1.tld>
       port   = 80
       listen = 0.0.0.0
       <redirect>
         / = https://vhost2.tld:443
       </redirect>
    </server>
  

Valid XHTML 1.0!