Heroku重定向非www到www on Django website

nwwlzxa7  于 9个月前  发布在  Go
关注(0)|答案(1)|浏览(60)

我目前有一个网站域名与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小时,我不能设法改变我的配置快。
感谢任何帮助提前!

zfycwa2u

zfycwa2u1#

所以我可以跟踪这个链接:
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)

我已经测试过了,它目前正在工作!我仍然会感谢任何反馈这种方法!

相关问题