📖 Scenario: You are building a simple REST API server that handles URL redirects. Some URLs should permanently redirect to a new location (301), while others should temporarily redirect (302).
🎯 Goal: Create a Python REST API using Flask that returns 301 or 302 redirects based on the requested URL.
📋 What You'll Learn
Create a dictionary called redirects with URLs as keys and tuples as values. Each tuple contains the target URL and the redirect type (301 or 302).
Create a variable called default_redirect that holds the default redirect URL and type.
Write a Flask route /redirect/<path> that checks if the path is in redirects and returns the correct redirect response.
If the path is not found, redirect to default_redirect URL with its redirect type.
Print the redirect status code and target URL in the response.
💡 Why This Matters
🌍 Real World
Web servers and APIs often need to redirect old URLs to new ones, either permanently (301) or temporarily (302). This helps users and search engines find the right pages.
💼 Career
Understanding how to implement redirects is important for backend developers, web developers, and DevOps engineers managing web traffic and SEO.
Progress0 / 4 steps
1
Create the redirects dictionary
Create a dictionary called redirects with these exact entries: 'old-home': ('/new-home', 301), 'temp-page': ('/temporary', 302), and 'old-contact': ('/contact-us', 301).
Rest API
Hint
Use a Python dictionary with keys as strings and values as tuples containing the target URL and redirect code.
2
Add the default redirect variable
Create a variable called default_redirect and set it to the tuple ('/home', 302).
Rest API
Hint
Set default_redirect to a tuple with the URL '/home' and redirect code 302.
3
Create the Flask route for redirects
Import Flask and create an app. Write a route @app.route('/redirect/<path>') with a function redirect_url(path) that checks if path is in redirects. If yes, get the target URL and code from redirects[path]. Otherwise, use default_redirect. Return a Flask redirect response with the target URL and status code.
Rest API
Hint
Use Flask's redirect() function with the target URL and status code.
4
Print redirect details on each request
Inside the redirect_url(path) function, before returning, add a print statement that outputs exactly: Redirecting to {target} with status {code} using an f-string.
Rest API
Hint
Use an f-string inside print() to show the target and code.
Practice
(1/5)
1. What does a 301 redirect indicate in a REST API?
easy
A. The resource has been deleted.
B. The resource is temporarily unavailable.
C. The resource has moved permanently to a new URL.
D. The resource requires authentication.
Solution
Step 1: Understand the meaning of 301 redirect
A 301 redirect means the resource has moved permanently to a new location.
Step 2: Compare with other options
Other options describe different HTTP statuses, not 301 redirect.
Final Answer:
The resource has moved permanently to a new URL. -> Option C
Quick Check:
301 redirect = permanent move [OK]
Hint: 301 means permanent move, 302 means temporary move [OK]
Common Mistakes:
Confusing 301 with 302 redirect
Thinking 301 means resource deleted
Assuming 301 is for authentication
2. Which HTTP status code is used for a temporary redirect?
easy
A. 301
B. 302
C. 404
D. 500
Solution
Step 1: Identify status codes for redirects
302 is the standard status code for temporary redirects.
Step 2: Eliminate other codes
301 is permanent redirect, 404 is not found, 500 is server error.
Final Answer:
302 -> Option B
Quick Check:
Temporary redirect = 302 [OK]
Hint: 302 means temporary redirect, not 301 [OK]
Common Mistakes:
Mixing 301 and 302 codes
Choosing 404 or 500 by mistake
Not knowing redirect codes
3. What will be the HTTP status code returned by this REST API snippet?
C. Status code 301 is correct for temporary redirect.
D. Status code should be 302 for temporary redirect.
Solution
Step 1: Check status code for temporary redirect
Temporary redirects require status code 302, not 301.
Step 2: Verify headers
Location header is present and correct, so no error there.
Final Answer:
Status code should be 302 for temporary redirect. -> Option D
Quick Check:
Temporary redirect = 302, not 301 [OK]
Hint: Use 302 status for temporary redirects, not 301 [OK]
Common Mistakes:
Using 301 instead of 302 for temporary redirects
Omitting Location header
Misunderstanding redirect status codes
5. You want to permanently redirect old API endpoints to new ones but keep temporary redirects for beta features. Which status codes should you use for each case?
hard
A. 301 for old endpoints, 302 for beta features
B. 302 for old endpoints, 301 for beta features
C. 404 for old endpoints, 302 for beta features
D. 500 for old endpoints, 301 for beta features
Solution
Step 1: Determine redirect type for old endpoints
Old endpoints moved permanently, so use 301 status code.
Step 2: Determine redirect type for beta features
Beta features are temporary, so use 302 status code.
Final Answer:
301 for old endpoints, 302 for beta features -> Option A