Update middleware.py

fixing redirect bug