Random 404 instead of 301 for URLs without trailing slash

221 vues
Accéder directement au premier message non lu

Frederik Elwert

non lue,
16 sept. 2016, 12:22:3216/09/2016
à Django users
Hello,

I am running a site that uses Django 1.8 and Django CMS 3.2. The site is deployed using gunicorn and nginx. Now I noticed a strange, hard to debug issue: When accessing a URL without trailing slash, I expect a redirect to the correct URL with the trailing slash appended, as per the APPEND_SLASH setting. This usually works, but sometimes I get a 404 instead of the correct 301. I can reproduce this using curl:

$ curl -I http://khk.ceres.rub.de/en/research/focus-groups/notions
HTTP/1.1 404 NOT FOUND
Server: nginx/1.6.2
Date: Fri, 16 Sep 2016 15:54:40 GMT
Content-Type: text/html
Connection: keep-alive
x-xss-protection: 1; mode=block
Content-Language: en
x-content-type-options: nosniff
Vary: Cookie
X-Frame-Options: SAMEORIGIN
Set-Cookie: django_language=en; expires=Sat, 16-Sep-2017 15:54:40 GMT; Max-Age=31536000; Path=/

$ curl -I http://khk.ceres.rub.de/en/research/focus-groups/notions
HTTP/1.1 301 MOVED PERMANENTLY
Server: nginx/1.6.2
Date: Fri, 16 Sep 2016 15:54:42 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
x-xss-protection: 1; mode=block
Content-Language: en
x-content-type-options: nosniff
Vary: Cookie
Location: http://khk.ceres.rub.de/en/research/focus-groups/notions/
X-Frame-Options: SAMEORIGIN
Set-Cookie: django_language=en; expires=Sat, 16-Sep-2017 15:54:42 GMT; Max-Age=31536000; Path=/

I am a bit clueless why this behaviour is so unpredictable. Any ideas what might be going on here, or how to trace this?

Thanks,
Frederik

Michal Petrucha

non lue,
16 sept. 2016, 12:40:1116/09/2016
à django...@googlegroups.com
Hi Frederik,
APPEND_SLASH only applies to URLs that do not match any URL pattern.
It is possible that you have a catch-all urlpattern that happens to
match your URL without a trailing slash, and then raise an explicit
404 – this might happen, for instance, if you're using something like
a wiki application, or a custom handler for flatpages, included under
a particular URL prefix.

Good luck,

Michal
signature.asc

Frederik Elwert

non lue,
19 sept. 2016, 05:04:2619/09/2016
à Django users
Hi Michal,

Thanks for your reply! APPEND_SLASH seemed to have an effect, but indeed, it might be Django-CMS hat mimics the behaviour. I still don’t understand why this behaviour is so unpredictable, but I’ll ask again on the Django CMS group.

Thanks again,
Frederik
Répondre à tous
Répondre à l'auteur
Transférer
0 nouveau message