简体   繁体   English

如何重定向到 www. Heroku 上我的 Flask 网站的版本?

[英]How do I redirect to the www. version of my Flask site on Heroku?

I've got a Python Flask app running on Heroku (Cedar stack) with two custom domains (one with and one without the www subdomain).我有一个 Python Flask 应用程序在 Heroku(Cedar 堆栈)上运行,它有两个自定义域(一个有一个没有 www 子域)。 I'd like to redirect all incoming requests to the www.我想将所有传入请求重定向到 www. version of the resource requested (the inverse of this question<\/a> ).请求的资源的版本( 这个问题<\/a>的反面)。 I think I need some WSGI middleware for this but I can't find a good example.我想我需要一些 WSGI 中间件,但我找不到一个很好的例子。

How do I do this?我该怎么做呢?

"

An easier solution than to create a separate Heroku app would be a before_request function. 比创建单独的Heroku应用程序更简单的解决方案是before_request函数。

from urlparse import urlparse, urlunparse

@app.before_request
def redirect_nonwww():
    """Redirect non-www requests to www."""
    urlparts = urlparse(request.url)
    if urlparts.netloc == 'example.com':
        urlparts_list = list(urlparts)
        urlparts_list[1] = 'www.example.com'
        return redirect(urlunparse(urlparts_list), code=301)

This will redirect all non-www requests to www using a "HTTP 301 Moved Permanently" response. 这将使用“HTTP 301 Moved Permanently”响应将所有非www请求重定向到www。

According to the Heroku Docs, you've got the right idea about using the www subdomain (eg www.foo.com) vs apex domain (eg foo.com). 根据Heroku Docs,你对使用www子域(例如www.foo.com)和apex域(例如foo.com)有了正确的想法。 Their suggestion for dealing with this is to use a DNS layer redirect: 他们处理此问题的建议是使用DNS层重定向:

To quote: 报价:

Subdomain redirection 子域重定向

Subdomain redirection results in a 301 permanent redirect to the specified subdomain for all requests to the apex domain so all current and future requests are properly routed and the full www hostname is displayed in the user's location field. 对于到顶点域的所有请求,子域重定向会导致301永久重定向到指定的子域,以便正确路由所有当前和将来的请求,并在用户的位置字段中显示完整的www主机名。

Almost all DNS providers offer domain redirection services - sometimes also called domain forwarding. 几乎所有DNS提供商都提供域重定向服务 - 有时也称为域转发。 DNSimple provides a convenient URL redirect seen here redirecting from the heroku-sslendpoint.com apex domain to the www.heroku-sslendpoint.com subdomain. DNSimple提供了一个方便的URL重定向,此处从heroku-sslendpoint.com apex域重定向到www.heroku-sslendpoint.com子域。

Source: http://devcenter.heroku.com/articles/avoiding-apex-domains-dns-arecords#subdomain_redirection 资料来源: http//devcenter.heroku.com/articles/avoiding-apex-domains-dns-arecords#subdomain_redirection

Hope that helps! 希望有所帮助!

One possible approach would be to add a function to listen on request_started , and do the appropriate redirection. 一种可能的方法是添加一个函数来侦听request_started ,并进行适当的重定向。

This signal is sent before any request processing started but when the request context was set up. 此信号在任何请求处理开始之前发送,但是在设置请求上下文时发送。 Because the request context is already bound, the subscriber can access the request with the standard global proxies such as request. 由于请求上下文已经绑定,因此订阅者可以使用标准全局代理(例如请求)访问请求。

我最终做的是创建第二个Heroku应用程序,将非www主机名分配给该主机并使用catch所有Flask路由重定向到www版本,保持路径不变。

Try this. 

@app.route('/', methods=['GET', 'POST'], subdomain="www")
@app.route('/<string:path>', methods=['GET', 'POST'], subdomain="www")
def www(path=''):
    return redirect(url_for('app.index')+path)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM