Redirects help send users or browsers from one web address to another automatically. 301 means the move is permanent, and 302 means it is temporary.
301 and 302 redirects in Rest API
HTTP/1.1 301 Moved Permanently Location: https://new-url.com HTTP/1.1 302 Found Location: https://temporary-url.com
The Location header tells the browser where to go next.
301 means permanent redirect, 302 means temporary redirect.
HTTP/1.1 301 Moved Permanently Location: https://example.com/newpage
HTTP/1.1 302 Found Location: https://example.com/temporarypage
This simple Python server sends a 301 redirect when you visit /oldpage and a 302 redirect when you visit /temp. Other paths show a welcome message.
from http.server import BaseHTTPRequestHandler, HTTPServer class RedirectHandler(BaseHTTPRequestHandler): def do_GET(self): if self.path == '/oldpage': self.send_response(301) self.send_header('Location', 'https://example.com/newpage') self.end_headers() elif self.path == '/temp': self.send_response(302) self.send_header('Location', 'https://example.com/temporarypage') self.end_headers() else: self.send_response(200) self.end_headers() self.wfile.write(b'Welcome to the server!') if __name__ == '__main__': server = HTTPServer(('localhost', 8000), RedirectHandler) print('Server running on http://localhost:8000') server.serve_forever()
Browsers cache 301 redirects, so changes may not show immediately.
Use 301 for permanent moves to help search engines update links.
Use 302 when you want to redirect temporarily without changing the original URL in search engines.
301 redirect means the page has moved permanently.
302 redirect means the page is temporarily moved.
Redirects help users and search engines find the right page automatically.
Practice
301 redirect indicate in a REST API?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 CQuick Check:
301 redirect = permanent move [OK]
- Confusing 301 with 302 redirect
- Thinking 301 means resource deleted
- Assuming 301 is for authentication
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 BQuick Check:
Temporary redirect = 302 [OK]
- Mixing 301 and 302 codes
- Choosing 404 or 500 by mistake
- Not knowing redirect codes
return Response(status=301, headers={'Location': 'https://newsite.com'})Solution
Step 1: Analyze the status code in the response
The code explicitly sets status=301, which means permanent redirect.Step 2: Check the Location header
The Location header points to the new URL, confirming redirect behavior.Final Answer:
301 Moved Permanently -> Option AQuick Check:
Status 301 with Location = permanent redirect [OK]
- Assuming 200 OK without checking status
- Confusing 301 with 302
- Ignoring Location header
return Response(status=301, headers={'Location': 'https://temp.example.com'})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 DQuick Check:
Temporary redirect = 302, not 301 [OK]
- Using 301 instead of 302 for temporary redirects
- Omitting Location header
- Misunderstanding redirect status codes
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 AQuick Check:
Permanent = 301, Temporary = 302 [OK]
- Swapping 301 and 302 codes
- Using error codes instead of redirects
- Not distinguishing permanent vs temporary
Ready to prove it?
Pass the test and this pattern moves unknown
