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

Django
2015-12-22 09:57 (10 years ago)
Using Django RedirectView Inline in URLs to Redirect While Preserving URL Path

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.

Categories

Archive