Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#19488 closed Bug (fixed)

Locale middleware with settings.APPEND_SLASH enabled is buggy (doesn't redirect) with valid URLs not ending with slash

Reported by: danger@… Owned by: nobody
Component: Core (Other) Version: 1.4
Severity: Normal Keywords: redirect, i18n, locale middleware
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hello,

the locale middleware does not handle the case when settings.APPEND_SLASH is enabled and a valid URL that doesn't end with slash is accessed. Example:

i18n url pattern ^sitemap\.xml$

I access domain.tld/sitemap.xml

The middleware checks if settings.APPEND_SLASH and not language_path.endswith('/') and in that case appends '/' to the url, so we get something like language_path=u'/en/sitemap.xml/'.

That in turn does not match any of the urls because our regexp doesn't allow slash at the end for sitemap.xml and we get 404 instead of redirect to /en/sitemap.xml.

My pull request is at https://github.com/django/django/pull/591

Attachments (3)

t19488.tar.gz (6.2 KB) - added by danger@… 2 years ago.
sample project
t19488.tar.2.gz (5.9 KB) - added by danger@… 2 years ago.
cleanup a bit.
19488-1.diff (2.7 KB) - added by claudep 2 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 Changed 2 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Could you paste your MIDDLEWARE_CLASSES setting?

comment:2 Changed 2 years ago by danger@…

Pretty standard:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware',
)

comment:3 Changed 2 years ago by russellm

  • Resolution set to worksforme
  • Status changed from new to closed

I haven't been able to reproduce this problem - an i18n URL pattern, ending in \.xml$ with APPEND_SLASH=True doesn't redirect for me. Feel free to reopen if you can provide a minimal test project that demonstrates the problem.

Changed 2 years ago by danger@…

sample project

comment:4 Changed 2 years ago by danger@…

  • Resolution worksforme deleted
  • Status changed from closed to new

I have attached a sample project which demonstrates my problem.

When you try accessing /index.html I belive it should redirect to /en/index.html the same way as it does when you access /ok and you get redirected to /en/ok/

Changed 2 years ago by danger@…

cleanup a bit.

comment:5 Changed 2 years ago by danger@…

additional note: when you define the url pattern straight in the top-level urls.py file (in i18n_patterns), the redirect works fine. The problem seems to start when you go through the include() statement.

Changed 2 years ago by claudep

comment:6 Changed 2 years ago by claudep

  • Triage Stage changed from Unreviewed to Accepted

I think I reproduced (and fixed) the issue in the attached patch. Compared to the pull request, I re-arranged some lines so as is_valid_path is not called twice if not necessary.

comment:7 Changed 2 years ago by Claude Paroz <claude@…>

  • Resolution set to fixed
  • Status changed from new to closed

In bcdb4898cae2f24599b39845b8e4cd7edc202424:

Fixed #19488 -- Made i18n_patterns redirect work with non-slash-ending paths

Thanks Daniel Gerzo for the report and the initial patch.

comment:8 Changed 2 years ago by Claude Paroz <claude@…>

In b3887ab98ac5317ebe7889e8b35752ebfd9740f9:

[1.5.x] Fixed #19488 -- Made i18n_patterns redirect work with non-slash-ending paths

Thanks Daniel Gerzo for the report and the initial patch.
Backport of bcdb4898c from master.

comment:9 Changed 2 years ago by anonymous

thanks!

comment:10 Changed 2 years ago by Claude Paroz <claude@…>

In b3887ab98ac5317ebe7889e8b35752ebfd9740f9:

[1.5.x] Fixed #19488 -- Made i18n_patterns redirect work with non-slash-ending paths

Thanks Daniel Gerzo for the report and the initial patch.
Backport of bcdb4898c from master.

Note: See TracTickets for help on using tickets.
Back to Top