如何在 Yaws/Erlang 中重写 URL

How to rewrite URL in Yaws/Erlang

如何在不包含扩展名的情况下访问 yaws 文件?说,

www.domain.com/listen.yaws => www.domain.com/listen

我无法从 yaws documentation/appmod.

中找到任何具体的文档

我觉得这个问题终于搞清楚了!

您可以在 the Yaws PDF documentation 的 "Arg Rewrite" 部分 (7.1.2) 中找到一个如何完成此操作的示例。将服务器配置中的变量 arg_rewrite_mod 设置为支持重写的 Erlang 模块的名称:

arg_rewrite_mod = my_rewriter

为了支持重写,my_rewriter 模块必须定义并导出一个 arg_rewrite/1 函数,将 #arg{} 记录作为其参数:

-module(my_rewriter).
-export([arg_rewrite/1]).

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

rewrite_pages() ->
    ["/listen"].

arg_rewrite(Arg) ->
    Req = Arg#arg.req,
    {abs_path, Path} = Req#http_request.path,
    case lists:member(Path, rewrite_pages()) of
        true ->
            Arg#arg{req = Req#http_request{path = {abs_path, Path++".yaws"}}};
        false ->
            Arg
    end.

该代码包含 yaws_api.hrl 以获取 #arg{} 记录定义。

rewrite_pages/0 函数 return 是必须重写以包含 ".yaws" 后缀的页面列表;在此示例中,它只是您在问题中提到的 /listen 页面。如果在 arg_rewrite/1 中我们在该列表中找到请求的页面,我们将 ".yaws" 附加到页面名称并将其包含在新的 #arg{} 中,我们 return 到 Yaws,然后继续根据新的 #arg{}.

调度请求