Flask Snippets

Snippets are unofficial and unmaintained.

This is an archived view of user-submitted snippets. Despite being hosted on the Flask site, they are not official. No Flask maintainer has curated or checked the snippets for security, correctness, or design.

Catch-All URL

Posted by Oli on 2011-07-15 @ 23:56 and filed in URLs

A simple way to create a Catch-All function which serves every URL including / is to chain two route filters. One for the root path '/' and one including a path placeholder for the rest.

We can't just use one route filter including a path placeholder because each placeholder must at least catch one character.

from flask import Flask
app = Flask(__name__)

@app.route('/', defaults={'path': ''})
def catch_all(path):
    return 'You want path: %s' % path

if __name__ == '__main__':

A little demonstration..

% curl          # Matches the first rule
You want path:  
% curl  # Matches the second rule
You want path: foo/bar

Hope it helps!

This snippet by Oli can be used freely for anything you like. Consider it public domain.