简体   繁体   English

如何将包含以“www”开头的URL的所有请求重定向到裸域?

[英]How do I redirect all requests with URLs beginning with “www” to a naked domain?

I have been successful in adding Heroku custom domains for my app. 我成功地为我的应用添加了Heroku自定义域名。 I'd like to know how to capture requests that begin with www and redirect them to the naked domain. 我想知道如何捕获以www开头的请求并将它们重定向到裸域。 For example, I have the following custom domains mapped to my Heroku app: 例如,我将以下自定义域映射到我的Heroku应用程序:

www.myapp.com
myapp.com

Requests for http://myapp.com and http://www.myapp.com are both successful, but the www stays on. http://myapp.comhttp://www.myapp.com请求都是成功的,但www仍然存在。

Objective 目的

I want all requests for http://www.myapp.com to be redirected to http://myapp.com . 我希望将http://www.myapp.com所有请求重定向到http://myapp.com This should also work for other paths, like http://www.myapp.com/some/foo/bar/path redirects to http://myapp.com/some/foo/bar/path . 这也适用于其他路径,例如http://www.myapp.com/some/foo/bar/path重定向到http://myapp.com/some/foo/bar/path I want something like this: http://www.stef.io and see how the www. 我想要这样的东西: http//www.stef.io并看看如何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. 到目前为止,我在Google上找到的说明是关于编辑我的.htaccess文件,但是我正在使用Flask框架上的Python应用程序在Heroku上运行。

The recommended solution for this is DNS level redirection (see heroku help ). 建议的解决方案是DNS级别重定向(请参阅heroku帮助 )。

Alternatively, you could register a before_request function: 或者,您可以注册before_request函数:

from urlparse import urlparse, urlunparse

@app.before_request
def redirect_www():
    """Redirect www requests to non-www."""
    urlparts = urlparse(request.url)
    if urlparts.netloc == '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. 这将使用“HTTP 301 Moved Permanently”响应将所有www请求重定向到非www。

I'm not used to Flask, but you could set up a route that matches /^www./ and then redirect it to the url without the "www". 我不习惯Flask,但你可以设置一个匹配/^www./的路由,然后将其重定向到没有“www”的url。

You may want to check http://werkzeug.pocoo.org/docs/routing/#custom-converters or http://werkzeug.pocoo.org/docs/routing/#host-matching 您可以查看http://werkzeug.pocoo.org/docs/routing/#custom-convertershttp://werkzeug.pocoo.org/docs/routing/#host-matching

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

相关问题 如何用最终重定向替换 HTML 中的所有 URL? - How do I replace all URLs in HTML with their final redirect? Azure linux python 上的 webapps 裸重定向到 www - Azure webapps on linux python redirect naked to www 将所有非 www 请求重定向到 bottle-py 中的 www - Redirect all non-www requests to www in bottle-py 如何将不同的域名请求重定向到Django中的相同IP - How do I redirect different domain names requests to the same ip in django 如何在没有Apache和Nginx的情况下将所有HTTP请求重定向到ELB中的HTTPS - How do I redirect all HTTP requests to HTTPS in ELB without Apache and Nginx 我怎样才能让 django 将所有未知的 url 重定向到`/` - How can I get django to redirect all unknown urls to `/` Google App Engine - Python中的裸域路径重定向 - Google App Engine - Naked Domain Path Redirect in Python 如何重定向到 www. Heroku 上我的 Flask 网站的版本? - How do I redirect to the www. version of my Flask site on Heroku? 如何在不使用 htaccess 的情况下将 HTTP 和 HTTPS 都重定向到 WWW? - How do I redirect both HTTP and HTTPS to WWW without using htaccess? 如何使用 requests_html 异步获取() URL 列表? - How do I use requests_html to asynchronously get() a list of URLs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM