We have a new wiki. The migration is not 100% complete. You can help out by moving pages across. This wiki will exist for as long as there are pages left.

The official documentation has moved to http://docs.couchdb.org — The transition is not 100% complete, but http://docs.couchdb.org should be seen as having the latest info. In some cases, the wiki still has some more or older info on certain topics inside CouchDB.

You need to be added to the ContributorsGroup to edit the wiki. But don't worry! Just email any Mailing List or grab us on IRC and let us know your user name.

This is an overview of http rewrite handler.

See also the official documentation for this topic.

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 :

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

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

This path is 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.




Rewrite to


{"from": "/a/b", "to": "/some/"}



k = v

{"from": "/a/b", "to": "/some/:var"}



var = b

{"from": "/a", "to": "/some/*"}



{"from": "/a/*", "to": "/some/*}



{"from": "/a", "to": "/some/*"}



{"from": "/a/:foo/*","to": "/some/:foo/*



foo = b

{"from": "/a/:foo", "to": "/some", "query": { "k": ":foo" }}



foo =:= b

{"from": "/a", "to": "/some/:foo" }



foo = b

Paths are relative to the design doc, so "/" mean the design doc too and "../" mean the path above the design doc and so on.

Rewriting_urls (last edited 2013-06-19 15:44:53 by 79)