【问题标题】:Heroku Redirect non-www to www on Django websiteHeroku 将非 www 重定向到 Django 网站上的 www
【发布时间】:2020-12-28 14:43:49
【问题描述】:

我目前有一个 Bluehost 的网站域。 Bluehost 无法使用 301 重定向将 https://example.com 重定向到 https://www.example.com,因为我没有托管它们。现在在我的 DNS 设置中,我有一个 CNAME (Alias) 记录将 @ 指向 <string1>.herokudns.com,我还有另一个 CNAME (Alias)www 指向 <string2>.herokudns.com。请注意,我有 SSL 证书,并且两者都在 HTTPS 上。

我已联系 Bluehost,他们说一种解决方案是将 www@ 都指向 <string1>.herokudns.com,但这似乎不是对我的重定向。

如何在 Python 上的 Heroku 中将裸域重定向到 www?我无法对它们进行大量测试,因为 Bluehost TTL 是 4 小时,而且我无法快速更改我的配置。

提前感谢任何帮助!

【问题讨论】:

    标签: django heroku


    【解决方案1】:

    所以我可以点击这个链接:

    https://adamj.eu/tech/2020/03/02/how-to-make-django-redirect-www-to-your-bare-domain/

    我将它定制为:

    from django.http import HttpResponsePermanentRedirect
    
    
    class WwwRedirectMiddleware:
        def __init__(self, get_response):
            self.get_response = get_response
    
        def __call__(self, request):
            host = request.get_host().partition(':')[0]
            if host == "example.com":
                return HttpResponsePermanentRedirect(
                    "https://www.example.com" + request.path
                )
            else:
                return self.get_response(request)
    

    我已经对其进行了测试,它目前有效!对于此方法的任何反馈,我仍将不胜感激!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 2017-04-13
      • 1970-01-01
      • 2020-04-12
      • 2020-03-15
      相关资源
      最近更新 更多