Rewrite HTTP redirect 301 to 302

Problem this snippet solves:

Intercepts pages with HTTP Status Code 301 (Permanent Redirect) & re-writes the Status Code as 302 (Temporary Redirect).

Special handling or an alternate approach may be required to preserve any cookies or other custom headers returned by the server.

Code :

# To redirect to the same location, same protocol:

when HTTP_RESPONSE {
  if { [HTTP::status] == "301" } {
    # respond with 302 using original Location value
    HTTP::respond 302 Location [HTTP::header Location]
  }
}

# To redirect to the same location but change protocol from HTTP to HTTPS:

when HTTP_RESPONSE {
  if { [HTTP::status] == "301" } {
    # respond with 302 using original Location value
    HTTP::respond 302 Location [string map { http:// https:// } [HTTP::header Location]]
  }
}
Published Mar 18, 2015
Version 1.0

Was this article helpful?

No CommentsBe the first to comment