简体   繁体   English

Heroku 将非 www 重定向到 Django 网站上的 www

[英]Heroku Redirect non-www to www on Django website

I currently have a website domain with Bluehost.我目前有一个 Bluehost 的网站域。 Bluehost cannot redirect https://example.com to https://www.example.com using 301 redirects since I am not hosting with them. Bluehost 无法使用 301 重定向将https://example.com重定向到https://www.example.com,因为我没有与他们一起托管。 Now in my DNS settings, I have a CNAME (Alias) record that points @ to <string1>.herokudns.com and I have another CNAME (Alias) that points www to <string2>.herokudns.com .现在在我的 DNS 设置中,我有一个CNAME (Alias)记录,将@指向<string1>.herokudns.com ,我有另一个CNAME (Alias)www指向<string2>.herokudns.com Please note that I have SSL certificate and both are on HTTPS.请注意,我有 SSL 证书并且都使用 HTTPS。

I have contacted Bluehost and they said one solution would be to point both www and @ to <string1>.herokudns.com but that does not seem to be a redirection to me.我已经联系了 Bluehost,他们说一种解决方案是将www@ 都指向<string1>.herokudns.com但这似乎不是对我的重定向。

How can I manage to redirect the naked domain to www within Heroku on Python?如何在 Python 上的 Heroku 中设法将裸域重定向到www I cannot test them a lot since Bluehost TTL is 4 hours and I cannot manage to change my configurations fast.我无法对它们进行大量测试,因为 Bluehost TTL 为 4 小时,而且我无法快速更改配置。

Appreciate any help in advance!提前感谢任何帮助!

So I was able to follow this link:所以我可以点击这个链接:

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

And I customized it like:我将它定制为:

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)

I have tested it and it currently works!我已经对其进行了测试,目前可以使用! I'd still appreciate any feedback on this method!我仍然感谢您对此方法的任何反馈!

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

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