1

How can I configure my Django project to redirect multiple slashes (symbol /) to a single slash?

For example, I want to issue a HTTP response code 301 redirect for this URL

https://mywebsite.com/path//to//id///

and redirect it to:

https://mywebsite.com/path/to/id/

How could that be done?

Ralf
  • 16,086
  • 4
  • 44
  • 68
Krishna Sunuwar
  • 2,915
  • 16
  • 24
  • this might help you: https://stackoverflow.com/questions/15638104/regex-for-matching-multiple-forward-slashes-in-url – paulsbecks Jul 08 '18 at 04:38
  • What WSGI server are you using? Production hosting mechanisms such as Apache/mod_wsgi will collapse such repeating slashes in paths and you will never see them, thus you don't need to have special handling for them. – Graham Dumpleton Jul 09 '18 at 22:22
  • @GrahamDumpleton , is it possible to issue 301 redirect in such cases? I'm using Apache/mod_wsgi. I want 301 redirect from example.com///////////// to example.com – Paul R Sep 05 '20 at 12:32

1 Answers1

1

In Django 2: using re_path

https://docs.djangoproject.com/en/2.2/ref/urls/#re-path

from django.urls import re_path

re_path(r'^bar/(?P<foo>[/\w]+)/$', views.bio, name='bio' ),
sandes
  • 1,917
  • 17
  • 28