Last updated on August 29th, 2024 at 11:35 am

In this tutorial we are going to walk through simple steps on how to redirect an URL in Flask Python with HTTP status code.

Before we start make sure that you import the redirect section from flask module

from flask import Flask, redirect

Redirect With Status Code

Below is an example on how to redirect an application route with status code to a different page.

@app.route('/direct.html')
def redirect_me():
    return redirect("/static/another.html", code=302)

As you can see above in our application we have an URL direct.html and when someone hit that URL it gets redirected to a different page within the same application that is /static/another.html. It also appends a response code accordingly. Here is the snippet from my application logs when I hit direct.html logs

127.0.0.1 - - [24/Jun/2022 15:57:14] "GET /direct.html HTTP/1.1" 302 -
127.0.0.1 - - [24/Jun/2022 15:57:14] "GET /static/another.html HTTP/1.1" 304 -

As you can see from the logs the first line says 302 HTTP response and the second line says 304 HTTP response.

Note: It is not mandatory to provide status=302 in the redirect statement. Even if we are not providing it redirect function will automatically add that.

Why 304 Not Modified HTTP response instead of 200 OK ?

This is simply because my browser already have the content basically it cached my result since I tested it before. Clear your cache to see the original result. It should be something like below in the first run/hit

127.0.0.1 - - [24/Jun/2022 16:11:46] "GET /direct.html HTTP/1.1" 302 -
127.0.0.1 - - [24/Jun/2022 16:11:46] "GET /static/another.html HTTP/1.1" 200 -

Redirect to a different website

In the example above we were dealing with redirection within the website itself. Now let’s take a look at how to redirect to an external website.

@app.route('/direct2.html')
def redirect_url():
    return redirect("https://webtutorials.dev")

When the user hit the above URL direct2.html they will automatically get redirected to a different website in this case it is https://webtutorials.dev. As you can see above in the return function I have purposefully not added any code=302 but still it make sure that the redirect HTTP status is appended accordingly in the log files and also

127.0.0.1 - - [24/Jun/2022 16:01:35] "GET /direct2.html HTTP/1.1" 302 -

Let us deep dive on what really is happening behind the scene, If you run a CURL command against the same URL this is what you will see (output truncated)

* HTTP 1.0, assume close after body
< HTTP/1.0 302 FOUND
< Content-Type: text/html; charset=utf-8
< Content-Length: 248
< Location: https://webtutorials.dev
< Server: Werkzeug/2.0.2 Python/3.9.12
< Date: Fri, 24 Jun 2022 20:21:06 GMT
<
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
* Closing connection 0
<p>You should be redirected automatically to target URL: <a href="https://webtutorials.dev">https://webtutorials.dev</a>. If not click the link.%