from django.http import HttpResponseRedirect

class RedirectOldDomainMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        host = request.get_host().split(':')[0].lower()  # حذف پورت احتمالی
        old_domains = ['technodehkade.ir', 'www.technodehkade.ir']
        new_domain = 'noavaranfardaa.ir'

        if host in old_domains:
            new_url = request.build_absolute_uri(request.get_full_path())
            new_url = new_url.replace(host, new_domain, 1)
            return HttpResponseRedirect(new_url)

        return self.get_response(request)
