You can add a 301 redirect in your Django "urls.py" file by defining a new URL pattern and using the "RedirectView" class to redirect the old URL with the query parameter to the new URL. Here's an example:
from django.urls import path, re_path
from django.views.generic import RedirectView
urlpatterns = [
# Your other URL patterns...
# Redirect URLs with ?page=x to the same URL without the query parameter
re_path(r'^old-url/$', RedirectView.as_view(url='/new-url/')),
re_path(r'^old-url/(?P<page>\d+)/$', RedirectView.as_view(url='/new-url/')),
]
In this example, the first "repath" pattern matches the URL "old-url/" without any query parameters, and simply redirects it to "new-url/". The second "repath" pattern matches URLs with the query parameter "?page=x", where "x" is any numeric value, and captures the value of "x" as a named parameter "page". It then redirects the URL to "new-url/" without the query parameter.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-11-05 11:00:00 +0000
Seen: 11 times
Last updated: May 22 '22
How can I install Beegfs on Ubuntu 22.04?
How can a .zip file from GitHub be loaded into Google Colab?
What is the process of using the Multmerge() function in r to combine files in a directory?
In Mac, what is the method to increase the privileges of an executable through setuid?
What can be done to resolve the issue with the Untracked working tree file named '._.git'?
What are the steps to restrict the overall file size of uploaded files in NestJS using multer?