Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a redirection response

Say if I put www.abc.com in the browser, the browser automatically gets redirected to www.xyz.com. I need to get that redirect url from server side. That is, if www.abc.com returns a redirect url www.xyz.com, how can I request this redirect URL (www.xyz.com) from the original URL (www.abc.com)?

like image 385
Rahatur Avatar asked Oct 01 '10 05:10

Rahatur


People also ask

How do you catch a redirect?

Type "cache:sitename.com" in the address bar of Chrome and press "Enter" where "sitename" is the URL that is generating the redirect. This will show you a cached version of the site on which you can use the Inspect Element pane to find and capture the redirect URL.

What is a redirect response?

In HTTP, redirection is triggered by a server sending a special redirect response to a request. Redirect responses have status codes that start with 3 , and a Location header holding the URL to redirect to. When browsers receive a redirect, they immediately load the new URL provided in the Location header.

How long does it take for a redirect to work?

If everything is configured properly, it takes about 30 minutes to start working. Read more about different types of redirects.

Is redirect a GET request?

A redirect is an Http response sent to the client. The response contains an Http Header called Location which must contain an absolute url. The client then issues a GET request against this url. So, no, POST is not an option.


1 Answers

Here's a snippet from a web crawler that shows how to handle redirects:

  HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
  webRequest.AllowAutoRedirect = false;  // IMPORTANT
  webRequest.UserAgent = ...;
  webRequest.Timeout = 10000;           // timeout 10s

  // Get the response ...
  using (webResponse = (HttpWebResponse)webRequest.GetResponse())
  {   
     // Now look to see if it's a redirect
     if ((int)webResponse.StatusCode >= 300 && (int)webResponse.StatusCode <= 399)
     {
       string uriString = webResponse.Headers["Location"];
       Console.WriteLine("Redirect to " + uriString ?? "NULL");
       ...
     }
  }
like image 126
Ian Mercer Avatar answered Sep 18 '22 16:09

Ian Mercer