Consider this HTTP response header snippet from a REST API:
HTTP/1.1 301 Moved Permanently Location: https://example.com/new-location
What status code does this response return?
Look at the first line of the HTTP response header.
The status code 301 means the resource has been moved permanently to a new URL.
A client receives this HTTP response:
HTTP/1.1 302 Found Location: https://example.com/temporary
What does the client do next?
Think about the difference between temporary and permanent redirects.
A 302 redirect tells the client to temporarily use the new URL but not to update bookmarks or cache the redirect.
Which reason best explains why a website owner should use a 301 redirect instead of a 302 redirect when moving a page permanently?
Think about how search engines treat permanent vs temporary redirects.
301 redirects tell search engines the page moved permanently and transfer ranking power to the new URL.
Given this Python Flask code snippet handling redirects:
from flask import Flask, redirect
app = Flask(__name__)
@app.route('/old')
def old():
return redirect('https://example.com/new', code=302)
with app.test_client() as client:
response = client.get('/old')
print(response.status_code, response.headers.get('Location'))What is printed?
from flask import Flask, redirect app = Flask(__name__) @app.route('/old') def old(): return redirect('https://example.com/new', code=302) with app.test_client() as client: response = client.get('/old') print(response.status_code, response.headers.get('Location'))
Check the redirect code argument in the redirect function.
The redirect function is called with code=302, so the response status is 302 with the Location header set.
Consider this pseudo-code for a REST API endpoint:
if request.path == '/start':
return redirect('/middle', code=302)
elif request.path == '/middle':
return redirect('/start', code=302)What is the main reason this code causes an infinite redirect loop?
if request.path == '/start': return redirect('/middle', code=302) elif request.path == '/middle': return redirect('/start', code=302)
Trace the redirects step-by-step.
The two endpoints redirect to each other, causing the client to bounce back and forth endlessly.