0

I have a Django website with activated translations (django.middleware.locale.LocaleMiddleware),

I someone requests a non-existing page:

https://example.com/nonexisting

Django then responds with:

HTTP/1.1 302 FOUND
Date: Fri, 02 Sep 2016 09:15:45 GMT
Server: Apache/2.4.7 (Ubuntu)
Vary: Cookie,Host
Location: https://example.com/de/nonexisting
Content-Type: text/html; charset=utf-8

HTTP/1.1 301 MOVED PERMANENTLY
Date: Fri, 02 Sep 2016 09:15:45 GMT
Server: Apache/2.4.7 (Ubuntu)
Vary: Cookie,Host
X-Frame-Options: SAMEORIGIN
Content-Language: de
Set-Cookie: django_language=de; expires=Sat, 02-Sep-2017 09:15:45 GMT; Max-Age=31536000; Path=/
Location: https://example.com/de/nonexisting/
Content-Type: text/html; charset=utf-8

HTTP/1.1 404 NOT FOUND
Date: Fri, 02 Sep 2016 09:15:45 GMT
Server: Apache/2.4.7 (Ubuntu)
Vary: Cookie,Host
X-Frame-Options: SAMEORIGIN
Content-Language: de
Set-Cookie: django_language=de; expires=Sat, 02-Sep-2017 09:15:45 GMT; Max-Age=31536000; Path=/
Content-Type: text/html; charset=utf-8

The user receives in this order: 302,301,404

How can I achieve that the user directly gets the 404?

caliph
  • 1,389
  • 3
  • 27
  • 52
  • I think this is caused by middleware ordering in your settings. See [docs for middleware ordering](https://docs.djangoproject.com/ja/1.9/ref/middleware/#middleware-ordering). – xyres Sep 02 '16 at 09:46
  • Also, are you using any wildcard `*` in your urls? – xyres Sep 02 '16 at 09:53
  • No wildcards. I used the standard ordering from the Django CMS installer: sessionMW, localeMW, commonMW – caliph Sep 02 '16 at 09:57
  • If someone requests `/existing`, do they get redirected to `/de/existing`? – xyres Sep 02 '16 at 10:05
  • Which Django version are you using? Is the 404 on the final url raised by the URL resolver or by the view? – knbk Sep 02 '16 at 10:50
  • @xyres; yes thats correct – caliph Sep 02 '16 at 10:56
  • @knbk: 1.8; How can I find this out? There is no view for the nonexisting url, so its definitely not one of my views. – caliph Sep 02 '16 at 10:57
  • You can try `django.core.urlresolvers.is_valid_path(path)`. You might need to set the active language to `de`. – knbk Sep 02 '16 at 11:24
  • @caliph Well, then that makes sense. Server returns `301` to redirect user from`/nonexisting` to `/de/nonexisting`, then it returns `302` to redirect user to `/de/nonexisting/` (note the slash `/` at the end) and finally a `404` because URL doesn't exist. You might need to make a manual check if the URL exists or not before redirecting the user to a language specific URL. – xyres Sep 02 '16 at 11:45
  • django.core.urlresolvers.is_valid_path(path) is false – caliph Sep 02 '16 at 13:03

0 Answers0