0
0
Flaskframework~10 mins

Trailing slash behavior in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Trailing slash behavior
Client requests URL
Check route with trailing slash?
Redirect to URL with slash
Client follows redirect
Return response
When a Flask route ends with a slash, Flask redirects requests without the slash to the slash version. If no slash, Flask serves directly or 404.
Execution Sample
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/hello/')
def hello():
    return 'Hello!'

# Request to '/hello' triggers redirect to '/hello/'
Defines a route with trailing slash; accessing URL without slash redirects to URL with slash.
Execution Table
StepRequested URLRoute DefinedMatch?ActionResponse
1/hello/hello/No exact matchRedirect to '/hello/'302 Redirect
2/hello//hello/MatchCall hello()200 OK with 'Hello!'
3/hello//helloNo route defined404 Not Found404 Error
💡 Execution stops after redirect or response is returned.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
Requested URLNone/hello/hello//hello/
Matched RouteNoneNone (redirect)/hello/None
ResponseNone302 Redirect200 OK 'Hello!'404 Not Found
Key Moments - 2 Insights
Why does Flask redirect from '/hello' to '/hello/' when the route ends with a slash?
Because the route is defined with a trailing slash, Flask expects the URL to have it. When missing, Flask sends a redirect to add the slash, as shown in execution_table step 1.
What happens if you define a route without a trailing slash and access it with a slash?
Flask does not redirect in this case and returns 404 Not Found, because the exact route with slash is not defined. This is the opposite behavior of trailing slash routes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what response does Flask send when requesting '/hello' if the route is defined as '/hello/'?
A302 Redirect to '/hello/'
B200 OK with 'Hello!'
C404 Not Found
D500 Internal Server Error
💡 Hint
Check execution_table row 1 for requested URL '/hello' and route '/hello/'
At which step does Flask serve the actual content 'Hello!'?
AStep 3
BStep 1
CStep 2
DNever
💡 Hint
Look at execution_table row 2 where requested URL matches route exactly
If the route was defined as '/hello' (no slash), what would happen when requesting '/hello/'?
AFlask redirects to '/hello'
BFlask returns 404 Not Found
CFlask serves the route directly
DFlask returns 500 Internal Server Error
💡 Hint
Refer to key_moments about no trailing slash route behavior
Concept Snapshot
Flask routes ending with a slash expect URLs with slash.
If URL misses slash, Flask redirects (302) to add it.
Routes without trailing slash do not redirect if slash is added.
This helps avoid duplicate URLs and keeps URLs consistent.
Remember: trailing slash means 'folder-like' URL; no slash means 'file-like' URL.
Full Transcript
In Flask, when you define a route with a trailing slash like '/hello/', Flask expects the URL to end with a slash. If a user requests '/hello' without the slash, Flask sends a 302 redirect to '/hello/'. The client then follows this redirect and Flask serves the content. If the route is defined without a trailing slash, Flask will not redirect if the URL has a slash and will return a 404 error instead. This behavior helps keep URLs consistent and avoids duplicate content. The execution table shows these steps clearly: first a redirect when the slash is missing, then serving the content when the URL matches exactly, or a 404 if no matching route exists.