Redirecting an old URL to a new one with Flask micro-framework

2024/5/20 18:06:45

I'm making a new website to replace a current one, using Flask micro-framework (based on Werkzeug) which uses Python (2.6 in my case).

The core functionality and many pages are the same. However by using Flask many of the previous URLs are different to the old ones.

I need a way to somehow store the each of the old URLs and the new URL, so that if a user types in an old URL they are simply forwarded to the new URL and everything works fine for them.



Does anybody know if this is possible in Flask?

Thank you in advance for your help :-)

Answer

Something like this should get you started:

from flask import Flask, redirect, requestapp = Flask(__name__)redirect_urls = {'http://example.com/old/': 'http://example.com/new/',...
}def redirect_url():return redirect(redirect_urls[request.url], 301)for url in redirect_urls:app.add_url_rule(url, url, redirect_url)
https://en.xdnf.cn/q/73324.html

Related Q&A

python decimals - rounding to nearest whole dollar (no cents) - with ROUND_HALF_UP

Im trying to use Decimal.quantize() to achieve the following: -For any amount of money, expressed as a python decimal of default precision, I want to round it using decimal.ROUND_HALF_UP so that it has…

How to use pytest fixtures in a decorator without having it as argument on the decorated function

I was trying to use a fixture in a decorator which is intended to decorate test functions. The intention is to provide registered test data to the test. There are two options:Automatic import Manual im…

Including Python standard libraries in your distribution [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about programming within the scope defined in the help center.Cl…

Using watchdog of python to monitoring afp shared folder from linux

I want linux machine(Raspberry pi) to monitor a shared folder by AFP(Apple file protocol, macbook is host).I can mount shared folder by mount_afp, and installed watchdog python library to monitor a sha…

Fitting curve: why small numbers are better?

I spent some time these days on a problem. I have a set of data:y = f(t), where y is very small concentration (10^-7), and t is in second. t varies from 0 to around 12000.The measurements follow an est…

Fast numpy roll

I have a 2d numpy array and I want to roll each row in an incremental fashion. I am using np.roll in a for loop to do so. But since I am calling this thousands of times, my code is really slow. Can you…

IndexError: fail to coerce slice entry of type tensorvariable to integer

I run "ipython debugf.py" and it gave me error message as belowIndexError Traceback (most recent call last) /home/ml/debugf.py in <module>() 8 fff = …

How to detect lines in noisy line images?

I generate noisy images with certain lines in them, like this one:Im trying to detect the lines using OpenCV, but something is going wrong.Heres my code so far, including the code to generate the noisy…

How can I connect a StringVar to a Text widget in Python/Tkinter?

Basically, I want the body of a Text widget to change when a StringVar does.

python csv writer is adding quotes when not needed

I am having issues with writing json objects to a file using csv writer, the json objects seem to have multiple double quotes around them thus causing the json objects to become invalid, here is the re…