Skip to content

URL Rewriting

Kelly McLaughlin edited this page Apr 25, 2013 · 1 revision

As of 1.10.0, Webmachine features the ability to rewrite incoming URLs and headers based on a set of rules. These rules can be defined in an Erlang module and the module name specified as part of the configuration passed into Webmachine when starting the web server. This functionality is very similar to mod_rewrite for those who may be familiar with that module for Apache httpd.


{rewrite_module, my_rewrite_rules_mod}

Webmachine calls the function rewrite/5 for each incoming request. Accordingly, each custom rewrite module must implement rewrite/5. The type spec for rewrite and a sample function skeleton are presented below. The headers may also be manipulated or augmented as part of the rewrite function.

-spec rewrite(atom(), atom(), {integer(), integer()}, gb_tree(), string()) ->
                     {gb_tree(), string()}.
rewrite(Method, Scheme, Vsn, Headers, Url) ->
    %% I likes my URLs backwards
    RewrittenPath = lists:reverse(Url),

    %% Add a new header
    RewrittenHeaders = mochiweb_headers:default("x-very-important-thing",
    {RewrittenHeaders, RewrittenPath}.

A more realistic example from Riak CS can be found here.

Something went wrong with that request. Please try again.