1 | initial version |
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.