couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <>
Subject [Couchdb Wiki] Update of "Rewriting_urls" by BenoitC
Date Sun, 31 Jan 2010 23:22:15 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.

The "Rewriting_urls" page has been changed by BenoitC.


New page:
= The http rewrite handler = 

The http rewrite handler. All rewriting is done from 
/dbname/_design/ddocname/_rewrite by default.

each rules should be in rewrites member of the design doc.
Ex of a complete rule :

    "rewrite": [ 
        "from": "", 
        "to": "index.html", 
        "method": "GET", 
        "query": {} 


 * '''from''': is the path rule used to bind current uri to the rule. It 
use pattern matching for that.

 * '''to''': rule to rewrite an url. It can contain variables depending on binding
variables discovered during pattern matching and query args (url args and from
the query member.)

 * '''method''': method to bind the request method to the rule. by default "*"
 * '''query''': query args you want to define they can contain dynamic variable
by binding the key to the bindings

to and from are path with  patterns. pattern can be string starting with ":" or
"*". ex:

This path are converted in erlang list by splitting "/". Each var are 
converted in atom. "*" is converted to '*' atom. The pattern matching is done
by splitting "/" in request url in a list of token. A string pattern will 
match equal token. The star atom ('*' in single quotes) will match any number 
of tokens, but may only be present as the last pathtern in a pathspec. If all 
tokens are matched and all pathterms are used, then the pathspec matches. It works
like webmachine. Each identified token will be reused in to rule and in query

The pattern matching is done by first matching the request method to a rule. by 
default all methods match a rule. (method is equal to "*" by default). Then
It will try to match the path to one rule. If no rule match, then a 404 error 
is displayed.

Once a rule is found we rewrite the request url using the "to" and
"query" members. The identified token are matched to the rule and 
will replace var. if '*' is found in the rule it will contain the remaining
part if it exists.


|| '''Match'''  || '''Url''' || '''Rewrite to''' || '''Tokens''' ||
|| {"from": "/a/b", "to": "/some/"} ||         /a/b?k=v ||        /some/b?k=v||        var
=:= b <br> k = v ||
|| {"from": "/a/b",  "to": "/some/:var"}   ||       /a/b ||             /some/b?var=b  ||
     var =:= b ||
|| {"from": "/a", "to": "/some/*"}||            /a ||               /some || ||
|| {"from": "/a/*", ||          /a/b/c ||          /some/b/c  || ||
|| {"from": "/a", "to": "/some/*"} ||         /a ||              /some  || ||
|| {"from": "/a/:foo/*",||     /a/b/c ||          /some/b/c?foo=b ||    foo =:= b ||
|| {"from": "/a/:foo", "to": "/some",  "query": {  "k": ":foo"  }} ||  /a/b ||          /some/?k=b&foo=b
||   foo =:= b ||
|| {"from": "/a",  "to": "/some/:foo" } ||        /a?foo=b  ||      /some/b  ||          
foo =:= b ||

View raw message