text: I am trying to redirect one page to another by using mitmproxy and Python. I can run my inline script together with mitmproxy without issues, but I am stuck when it comes to changing the URL to another URL. Like if I went to google.com it would redirect to stackoverflow.com
  def response(context, flow):
  print("DEBUG")
  if flow.request.url.startswith(""):
  print("It does contain it")
  flow.request.url = ""
  print("It does contain it")
  flow.request.url = ""
  print("DEBUG")
  if "google.com" in flow.request.url
  google.com
  Setting the
  url attribute will not help you, as it is merely constructed from underlying data.
  Depending on what exactly you want to accomplish, there are two options.
  (1) You can send an actual HTTP redirection response to the client. Assuming that the client understands HTTP redirections, it will submit a new request to the URL you give it.
  from mitmproxy.models import HTTPResponse from netlib.http import Headers def request(context, flow): if flow.request.host == 'google.com': flow.reply(HTTPResponse('HTTP/1.1', 302, 'Found', Headers(Location='', Content_Length='0'), b''))
  (2) You can silently route the same request to a different host. The client will not see this, it will assume that it’s still talking to
  google.com.
  def request(context, flow): if flow.request.url == '': flow.request.host = 'stackoverflow.com' flow.request.path = '/users/'
  These snippets were adapted from an example found in mitmproxy’s own GitHub repo. There are many more examples there.
  For some reason, I can’t seem to make these snippets work for Firefox when used with TLS (
  https://), but maybe you don’t need that.