##  Routing  Â¶

Modern web applications use meaningful URLs to help users. Users are more
likely to like a page and come back if the page uses a meaningful URL they can
remember and use to directly visit a page.

Use the [ ` route()  ` ](../api/#flask.Flask.route "flask.Flask.route")
decorator to bind a function to a URL.

    
    
    @app.route('/')
    def index():
        return 'Index Page'
    
    @app.route('/hello')
    def hello():
        return 'Hello, World'
    

You can do more! You can make parts of the URL dynamic and attach multiple
rules to a function.

###  Variable Rules  Â¶

You can add variable sections to a URL by marking sections with `
<variable_name> ` . Your function then receives the ` <variable_name> ` as a
keyword argument. Optionally, you can use a converter to specify the type of
the argument like ` <converter:variable_name> ` .

    
    
    from markupsafe import escape
    
    @app.route('/user/<username>')
    def show_user_profile(username):
        # show the user profile for that user
        return 'User %s' % escape(username)
    
    @app.route('/post/<int:post_id>')
    def show_post(post_id):
        # show the post with the given id, the id is an integer
        return 'Post %d' % post_id
    
    @app.route('/path/<path:subpath>')
    def show_subpath(subpath):
        # show the subpath after /path/
        return 'Subpath %s' % escape(subpath)
    

Converter types:

` string  ` |  (default) accepts any text without a slash   
---|---  
` int  ` |  accepts positive integers   
` float  ` |  accepts positive floating point values   
` path  ` |  like ` string  ` but also accepts slashes   
` uuid  ` |  accepts UUID strings   
  
###  Unique URLs / Redirection Behavior  Â¶

The following two rules differ in their use of a trailing slash.

    
    
    @app.route('/projects/')
    def projects():
        return 'The project page'
    
    @app.route('/about')
    def about():
        return 'The about page'
    

The canonical URL for the ` projects  ` endpoint has a trailing slash. Itâs
similar to a folder in a file system. If you access the URL without a trailing
slash, Flask redirects you to the canonical URL with the trailing slash.

The canonical URL for the ` about  ` endpoint does not have a trailing slash.
Itâs similar to the pathname of a file. Accessing the URL with a trailing
slash produces a 404 âNot Foundâ error. This helps keep URLs unique for
these resources, which helps search engines avoid indexing the same page
twice.

###  URL Building  Â¶

To build a URL to a specific function, use the [ ` url_for()  `
](../api/#flask.url_for "flask.url_for") function. It accepts the name of the
function as its first argument and any number of keyword arguments, each
corresponding to a variable part of the URL rule. Unknown variable parts are
appended to the URL as query parameters.

Why would you want to build URLs using the URL reversing function [ `
url_for()  ` ](../api/#flask.url_for "flask.url_for") instead of hard-coding
them into your templates?

  1. Reversing is often more descriptive than hard-coding the URLs. 

  2. You can change your URLs in one go instead of needing to remember to manually change hard-coded URLs. 

  3. URL building handles escaping of special characters and Unicode data transparently. 

  4. The generated paths are always absolute, avoiding unexpected behavior of relative paths in browsers. 

  5. If your application is placed outside the URL root, for example, in ` /myapplication  ` instead of ` /  ` , [ ` url_for()  ` ](../api/#flask.url_for "flask.url_for") properly handles that for you. 

For example, here we use the [ ` test_request_context()  `
](../api/#flask.Flask.test_request_context "flask.Flask.test_request_context")
method to try out [ ` url_for()  ` ](../api/#flask.url_for "flask.url_for") .
[ ` test_request_context()  ` ](../api/#flask.Flask.test_request_context
"flask.Flask.test_request_context") tells Flask to behave