如何重定向所有请求,URL以“www”开头到裸体域?

编程入门 行业动态 更新时间:2024-10-27 03:33:54
本文介绍了如何重定向所有请求,URL以“www”开头到裸体域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经成功地为我的应用添加了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 myapp

Requests for myapp and www.myapp are both successful, but the www stays on.

Objective

I 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”开头到裸体域?

本文发布于:2023-10-31 17:25:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546768.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:开头   重定向   URL   www

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!