网页有一个重定向循环

我正在为用户开发这个django应用程序,我打算让每个用户拥有自己的自定义子域。 我打算为此使用中间件。 到目前为止,这是我得到的,但是在访问该网站(在Google Chrome上),我得到了重定向循环错误:

class SubdomainMiddleware(object):

    def process_request(self, request):
        if request.user.is_authenticated():
            if request.company is None:
                company_subdomain = request.user.company.company_url
            else:
                company_subdomain = request.company.company_url
            company_subdomain = company_subdomain.lower()
            domain_parts = request.get_host().split('.')
            if len(domain_parts) > 2:
                if domain_parts[0].lower() != company_subdomain:
                    domain_parts[0] = company_subdomain
            else:
                domain_parts.insert(0, company_subdomain)
            domain = '.'.join(domain_parts)
            return HttpResponseRedirect('http://' + domain + request.get_full_path())
        else:
            pass

我做错了什么?


您的第一次重定向是正确的,但是即使您位于正确的域名上,也会再次重定向。 这导致重定向循环。 您需要检查当前子域是否与预期的子域匹配,如果是,则返回None而不是重定向:

if len(domain_parts) > 2:
    if domain_parts[0].lower() != company_subdomain:
        domain_parts[0] = company_subdomain
    else:  # Subdomain matches, return instead of redirect
        return None
else:
    domain_parts.insert(0, company_subdomain)
链接地址: http://www.djcxy.com/p/33735.html

上一篇: Web page has a redirect loop

下一篇: Django CSRF. Can I POST from subdomain to main domain?