Skip to content

Normalize URLs with extra slashes#2485

Open
vidhiii1711 wants to merge 4 commits intodjango:mainfrom
vidhiii1711:fix-normalize-extra-slashes-v2
Open

Normalize URLs with extra slashes#2485
vidhiii1711 wants to merge 4 commits intodjango:mainfrom
vidhiii1711:fix-normalize-extra-slashes-v2

Conversation

@vidhiii1711
Copy link
Copy Markdown

Fixes #2067

This PR adds middleware to normalize URLs containing extra slashes
(e.g. //community///community/) so links and assets resolve correctly.

Includes tests to ensure correct redirects on www.djangoproject.com.

Copy link
Copy Markdown
Contributor

@codingjoe codingjoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi there 👋,

I had some time and was browsing for reviews. I hope you don't mind me leaving some notes.

Cheerio!
Joe

@vidhiii1711
Copy link
Copy Markdown
Author

@codingjoe
Hi, thanks a lot for taking the time to review this and for the helpful suggestions!

I’ll update the implementation to simplify the slash normalization (likely using regex as you suggested) and reduce branching in the middleware.

I’ll also explore the try/except approach so the normalization only happens when a 404 occurs, keeping the common path fast.

Additionally, I’ll add a few adverse test cases to better cover edge scenarios.

Thanks again for the feedback — I’ll push an update shortly.

@vidhiii1711
Copy link
Copy Markdown
Author

I have updated the middleware to normalize multiple slashes in the URL using a regex .

I also added a test case in test_www_urls.py to ensure that URLs like /community// correctly redirect to /community/ with a 301 status.

Please let me know if any further changes are needed. Thanks again for the guidance!

Copy link
Copy Markdown
Contributor

@codingjoe codingjoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slick solution! LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The links and images do not work when extra slashes are in url

2 participants