'Django returning HTTP 301?

I have a django view that returns HTTP 301 on a curl request:

grapefruit:~ pete$ curl -I http://someurl
HTTP/1.1 301 MOVED PERMANENTLY
Date: Fri, 16 Oct 2009 19:01:08 GMT
Server: Apache/2.2.9 (Win32) mod_wsgi/2.5 Python/2.6.2 PHP/5.2.6
Location: http://someurl
Content-Type: text/html; charset=utf-8

I can't get the page's content from curl. However, if I visit the page with a browser, I see the content as expected.

Any ideas?

Thanks, Pete



Solution 1:[1]

My problem was PREPEND_WWW, when using it in localhost it returns a 301 error, I just created a local settings file and set it PREPEND_WWW = False

Solution 2:[2]

In my case, commenting the SecurityMiddleware on the settings.py file did the trick. I have to say that this only happens when running the runserver command (aka. when testing). With Apache it doesn't falls into the redirect loop.

Solution 3:[3]

So for me, it was a combination of the following:

  • SecurityMiddleware enabled
  • SECURE_SSL_REDIRECT = True
  • Separate Web Server had SSL redirect enabled as well

In that case, the web server already does the redirecting and forwards the request via HTTP to Django, which then redirects again to HTTPS which ends in an infinite loop.

So, in that case, it's safe to set SECURE_SSL_REDIRECT = False.

Solution 4:[4]

When using postman everything was working okay but when i was requesting it from the frontend i was getting the 301 error. You are probably missing the trailing slash in the url, i had the same problem and i solved it by adding the slash to the url....

Solution 5:[5]

Yes, please check URL you may be missing slash some where. I fixed this by adding slash at the end of URL

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AbeEstrada
Solution 2 Hassek
Solution 3 jaw
Solution 4 frimogen
Solution 5 Vicky Sharma