79

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

slypete
  • 5,538
  • 11
  • 47
  • 64

6 Answers6

229

You are probably requesting the URL without a trailing slash, and have APPEND_SLASH set to True (the default) in settings.py, so Django is redirecting to the URL including a slash.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 6
    The funny thing is that from Postman there is no such problem(301). I guess Postman adds the slash. – Simon Moshenko Apr 01 '18 at 21:18
  • What happened for me is that I needed to add an `Authorization` header on my requests. On safari, it would 301 and send back as unauthorized because it wouldn't carry over the header when the redirect happened. I was used to Chrome and Safari doing this automatically. Adding the slash worked for me. – Lewis Menelaws Apr 06 '20 at 04:17
5

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

AbeEstrada
  • 1,090
  • 1
  • 13
  • 15
5

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.

jaw
  • 932
  • 2
  • 10
  • 24
3

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.

Hassek
  • 8,715
  • 6
  • 47
  • 59
  • In my case, it happened on prod with gunicorn. The server returns the exact same Location as passed wich turns into an endless loop. I worked-around that issue by only setting `SECURE_SSL_REDIRECT = False` instead of completely removing the SecurityMiddleware. But I am wondering why that happens and how to actually fix it? – jaw Dec 16 '20 at 21:49
0

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....

-1

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

  • 1
    Please do not repeat other's answers. This is a duplicate of [older answer](https://stackoverflow.com/a/1579859/14401160), which is more detailed and is >10 years old. If you want to support an existing answer, please upvote it. – STerliakov Jun 12 '22 at 09:27