Flask 301响应 [英] Flask 301 Response

查看:291
本文介绍了Flask 301响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的烧瓶应用程序正在为其中一个网址做 301 重定向。



New Relic是:
$ b $ pre $ Traceback(最近一次调用最后一次):
文件/ var / www / app / env / local /lib/python2.7/site-packages/flask/app.py,第1358行,在full_dispatch_request
rv = self.dispatch_request()
文件/ var / www / app / env / local /lib/python2.7/site-packages/flask/app.py,第1336行,在dispatch_request
self.raise_routing_exception(req)
文件/ var / www / app / env / local / lib / python2.7 / site-packages / flask / app.py,第1319行,在raise_routing_exception中
raise request.routing_exception
RequestRedirect:301:永久移动

它看起来不像是我的代码,或者说回溯没有显示我的任何文件。有一点我有Nginx的所有非SSL请求重定向到HTTPS,但不得不禁用,因为Varnish无法使请求端口 443 出了一个错误.. 。可能一些配置,我做或不做。



它并不总是返回一个 301 ,我可以请求网址,并得到它没有任何问题。但是有人在请求URL的时候得到了一个 301 的响应。



这是一个 GET 请求与一些自定义标题链接到该帐户。



在我的代码没有任何一点是有一个 301 重定向。

解决方案

回溯显示它是引发重定向的路由匹配; 通常(例如,除非您添加了明确的重定向路由),这意味着客户端试图访问分支 URL(以结尾的斜杠 ),但是请求的URL不包含最后的斜杠。客户端只是用斜杠重定向到标准分支URL



Werkzeug Rule 文档


以斜杠结尾的URL规则是分支URL,其他是分支。如果启用了 strict_slashes (这是默认设置),那么匹配的所有分支URL都不会有结尾的斜杠,将会触发重定向到同一个URL,并附加缺少的斜杠。 / p>

路由文档


Flask的URL规则基于Werkzeug的路由模块。这个模块背后的想法是确保美丽和独特的网址,基于Apache和早期的HTTP服务器奠定了先例。

采取这两个规则:

  @ app.route('/ projects /')
def projects():
return'The project page'

@ app.route('/ about')
def about():
return'about page'

尽管它们看起来非常相似,但它们在URL定义中使用尾部斜线的方式有所不同。在第一种情况下,项目端点的规范URL具有尾部斜线。从这个意义上说,它类似于文件系统上的文件夹。在不使用尾部斜线的情况下访问它将导致Flask重定向到规范URL,并以斜线结束。



然而,在第二种情况下,URL没有定义斜线,而非像类UNIX系统上文件的路径名。访问带有斜线的URL会产生一个404Not Found的错误。



这种行为允许相对URL继续工作,即使尾部斜线被忽略, Apache和其他服务器如何工作。此外,这些网址将保持唯一,这有助于搜索引擎避免两次索引相同的页面。


如文档所述,如果您执行不是需要这个行为(并且没有结尾的斜杠是404 Not Found),你必须设置 strict_slashes = False 选项在您的路线。


My flask app is doing a 301 redirect for one of the urls.

The traceback in New Relic is:

Traceback (most recent call last):
  File "/var/www/app/env/local/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
  File "/var/www/app/env/local/lib/python2.7/site-packages/flask/app.py", line 1336, in dispatch_request
    self.raise_routing_exception(req)
  File "/var/www/app/env/local/lib/python2.7/site-packages/flask/app.py", line 1319, in raise_routing_exception
    raise request.routing_exception
RequestRedirect: 301: Moved Permanently

It doesn't look like it is even hitting my code or rather the traceback isn't showing any of my files in it. At one point I did have Nginx redirect all non SSL request to HTTPS but had to disable that as Varnish was not able to make the request to port 443 with out an error... probably some configuration that I did or didn't make.

It doesn't always return a 301 though, I can request the URL and get it without any trouble. But someone out in the world requesting the URL is getting a 301 response.

It is a GET request with some custom headers to link it to the account.

At no point in my code is there a 301 redirect.

解决方案

The traceback shows that it was the route matching that raised a redirect; usually (e.g. unless you added explicit redirect routes), that means the client tried to access a branch URL (one that ends with a trailing slash), but the requested URL did not include the last slash. The client is simply being redirected to the canonical branch URL with the slash.

From the Werkzeug Rule documentation:

URL rules that end with a slash are branch URLs, others are leaves. If you have strict_slashes enabled (which is the default), all branch URLs that are matched without a trailing slash will trigger a redirect to the same URL with the missing slash appended.

From the routing documentation:

Flask’s URL rules are based on Werkzeug’s routing module. The idea behind that module is to ensure beautiful and unique URLs based on precedents laid down by Apache and earlier HTTP servers.

Take these two rules:

@app.route('/projects/')
def projects():
    return 'The project page'

@app.route('/about')
def about():
    return 'The about page'

Though they look rather similar, they differ in their use of the trailing slash in the URL definition. In the first case, the canonical URL for the projects endpoint has a trailing slash. In that sense, it is similar to a folder on a file system. Accessing it without a trailing slash will cause Flask to redirect to the canonical URL with the trailing slash.

In the second case, however, the URL is defined without a trailing slash, rather like the pathname of a file on UNIX-like systems. Accessing the URL with a trailing slash will produce a 404 "Not Found" error.

This behavior allows relative URLs to continue working even if the trailing slash is ommited, consistent with how Apache and other servers work. Also, the URLs will stay unique, which helps search engines avoid indexing the same page twice.

As documented, if you do not want this behaviour (and have the url without the trailing slash be a 404 Not Found instead), you must set the strict_slashes=False option on your route.

这篇关于Flask 301响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆