Using Django RedirectView Inline in URLs to Redirect While Preserving URL Path

Django
2015-12-22 18:57 (8 years ago) ytyng

I want to redirect people who access /url-path-before/feature/hoge/ to /url-path-after/feature/hoge/.

Using URLs, you can do it like this:

url(r'^url-path-before/(?P<path>.*)',
    RedirectView.as_view(url="/url-path-after/%(path)s", permanent=False),
    name="after"),

This will enable the redirection.

If you set permanent=False, it will be a 302 redirect.

If you don't specify, permanent=True will be used, resulting in a 301 redirect.

Since 301 redirects can be cached by the browser and cause inconvenience, it might be a good idea to start with permanent=False for testing. If there are no issues with the behavior, you can remove it later.

Currently unrated

Comments

Archive

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011