If you don't have access to the webserver, and the only way you have is to intercept the request and then do a redirect from http to https, what is the best approach to redirect?
I tried looking at subscribers using NewRequest. I did something like so:
@subscriber(NewRequest) def modify_protocol(event): if re.search('some string', event.request.host_url): event.request.scheme = 'https'
However, this didn't do what I expected. The page is still being rendered. Any ideas would be appreciated.
Thanks in advance.
Inside a view:
if req.scheme == "http":
return HTTPFound("https://" + req.host + req.path_qs)
Using an event listener:
@subscriber(NewRequest)
def redirect(event):
if event.request.scheme == "http":
raise HTTPFound("https://" + event.request.host + event.request.path_qs)
I looked into approaches using send and get_response, but couldn't find much.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With