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



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

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

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

提前感谢任何帮助!

所以我能够点击这个链接:

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)

我已经测试过了,它目前可以工作!我仍然希望对这种方法有任何反馈!

最新更新