Default django redirect app middleware is not support url parameters. It will throw 404 error when URL have parameters.
This custom-redirect-middleware introduced to handle url parameters. So, this custom middleware will handle all the below cases with/without trailing slash in URL.
-
URL with no parameters.
-
URL with one parameter.
-
URL with more than one parameter.
-
Simply separate URL parameters from
full_absolute_path
. -
Next get exact
redirect_path
of separatedfull_absolute_path
formdjango.contrib.redirects database
. -
Then append the URL parameters which separated from
full_absolute_path
is toredirect_path
.
Steps to use custom redirect middleware :
-
Download custom redirect middleware custom redirect middleware
-
Place inside your Django app.
-
Replace
'django.contrib.redirects.middleware.RedirectFallbackMiddleware'
or add bellow line inMIDDLEWARE
of yoursettings.py
.
Note that the order of MIDDLEWARE
matters. Generally, you can put RedirectMiddleware
at the end of the list, because it’s a last resort. Refer here.
'your_django_app_name.custom_redirect_middleware.CustomRedirectFallbackMiddleware'
Thats it.
Inspired by this stackoverflow question.