Iis – In IIS 7, How Do I Redirect And Capture the Original URL

iisredirect

I have a peculiar CGI script written in Python; let's call it peculiar.py, I would like to "answer" any and all HTTP requests to a particular directory and anything under it, then capture the original URL from the environment variables. The original URL is to be used later. So far, I am trying a redirect to a default document, then calling up the environment variables for something relevant. I haven't found anything relevant.

The concept walkthrough goes like:

  1. User visits a URL like http://site.domain.tld/videos/fun-stuff.html.
  2. User clicks on a link that says "Funny Link of the Day," which has an href to http://site.domain.tld/redirect-source/2013-01-10/.
  3. IIS 7 receives request for http://site.domain.tld/redirect-source/2013-01-10/.
  4. IIS 7 redirects to http://site.domain.tld/redirect-target/.
  5. Peculiar.py, as the default document in http://site.domain.tld/redirect-target/, wakes up.
  6. Peculiar.py finds that the original URL was http://site.domain.tld/redirect-source/2013-01-10/.
  7. More processing after that.

My initial steps were to:

  1. Make peculiar.py the default document in the "redirect-target" directory, so that http://site.domain.tld/redirect-target/ would be answered by http://site.domain.tld/redirect-target/peculiar.py. This works.

  2. Use the HTTP Redirect feature, so http://site.domain.tld/redirect-source/subdir1/item1.ext would be redirected to http://site.domain.tld/redirect-target/ and answered by http://site.domain.tld/redirect-target/peculiar.py. This works.

  3. Try to get the environment variables and find a good referrer. (Or referer, as the grandfathered-in misspelling continues)

Right now, peculiar.py just grabs all of the environment variables and spits them out as HTML for me so I can examine them.

What I found, when I set up an HTTP Redirect using Found (302), Permanent (301), or even Temporary (307), I could manage the redirect, but I could not get the original URL. HTTP_REFERER isn't showing in the list of environment variables at all if I visit http://site.domain.tld/redirect-source/subdir1/item1.ext. HTTP_REFERER does appear in the list of environment variables if I embed http://site.domain.tld/redirect-source/subdir1/item1.ext in a webpage as a hyperlink, but it only shows the original webpage.

It appears as if redirection just … skips the referrer. Is it because the referer is passed along from the client, not the server?

The background is that I'm building a script that spits out things like reference movies for Quicktime (binary file, constructed on the fly) and QTL files, both of which would vary based on the original URL called. Yes, this is a lot of work to avoid querystrings in links.

Am I missing something obvious here? Is redirection the wrong tool for answering all URLs under a particular directory? Am I missing a setting in web.config, of which I am mostly ignorant?

I'm open to other approaches, so long as I can use Python and IIS.

Best Answer

I think your problem is that it is undefined as whether or not to send the Referer header in response to a HTTP/301 or HTTP/302. Remember that, in HTTP Redirection, the server sends the 301/302 with new Location header, but the browser is the thing that re-requests (and it decides whether or not to add the Location header).

Your best option might be pass the original value of Referer back in the 301/302 as a querystring parameter, e.g. (pseudo):

HTTP/1.1 302 Found
Location: http://site.domain.tld/redirect-target/?orig=http://site.domain.tld/redirect-source/2013-01-10/

and then capture the value of orig in your Python script.

You might need a custom ASP to do the actual redirection, using Response.Redirect(), rather than relying on the built-in IIS feature.

More: https://stackoverflow.com/questions/2158283/will-a-302-redirect-maintain-the-referer-string