Random 404 instead of 301 for URLs without trailing slash

221 views
Skip to first unread message

Frederik Elwert

unread,
Sep 16, 2016, 12:22:32 PM9/16/16
to 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

unread,
Sep 16, 2016, 12:40:11 PM9/16/16
to 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

unread,
Sep 19, 2016, 5:04:26 AM9/19/16
to 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
Reply all
Reply to author
Forward
0 new messages