我已经成功地为我的应用添加了Heroku自定义域。我想知道如何捕获以 www 开始的请求,并将其重定向到裸域。例如,我有以下自定义域映射到我的Heroku应用程序:
www.myapp myapp。 com请求 myapp 和 www.myapp 都是成功的,但 www p> 目标
我希望所有请求 www.myapp 被重定向到 myapp 。这也适用于其他路径,例如 www.myapp/some/foo/bar/path 重定向到 http:/ /myapp/some/foo/bar/path 。我想要这样的: www.stef.io ,看看 www。已从地址栏中删除。
目前为止,Google发现的说明是关于编辑我的 .htaccess 文件,但是我在Flask框架上使用Python应用程序在Heroku上运行。
解决方案推荐的解决方案是DNS级别重定向(请参阅 heroku帮助)。
或者,您可以注册一个 before_request 函数:
from urlparse import urlparse,urlunparse @ app.before_request def redirect_www(): 重定向www请求到非www。 urlparts = urlparse(request.url)如果urlpartsloc =='www.stef.io': urlparts_list =列表(urlpart urlparts_list [1] ='stef.io' return redirect(urlunparse(urlparts_list),code = 301)这将使用HTTP 301 Moved Permanently响应将所有www请求重定向到非www。
I have been successful in adding Heroku custom domains for my app. I'd like to know how to capture requests that begin with www and redirect them to the naked domain. For example, I have the following custom domains mapped to my Heroku app:
www.myapp myappRequests for myapp and www.myapp are both successful, but the www stays on.
ObjectiveI want all requests for www.myapp to be redirected to myapp. This should also work for other paths, like www.myapp/some/foo/bar/path redirects to myapp/some/foo/bar/path. I want something like this: www.stef.io and see how the www. is gone from the address bar.
The instructions I've found on Google so far are about editing my .htaccess file, but I'm running on Heroku with a Python app on the Flask framework.
解决方案The recommended solution for this is DNS level redirection (see heroku help).
Alternatively, you could register a before_request function:
from urlparse import urlparse, urlunparse @app.before_request def redirect_www(): """Redirect www requests to non-www.""" urlparts = urlparse(request.url) if urlpartsloc == 'www.stef.io': urlparts_list = list(urlparts) urlparts_list[1] = 'stef.io' return redirect(urlunparse(urlparts_list), code=301)This will redirect all www requests to non-www using a "HTTP 301 Moved Permanently" response.
更多推荐
如何重定向所有请求,URL以“www”开头到裸体域?
发布评论