CGI redirection: let us discuss it further

Discussion in 'Python' started by Sullivan WxPyQtKinter, Mar 27, 2006.

  1. I am now programming python scripts for CGI environment. The
    redirection has been discussed in this forum for over one hundred
    times. I have seen most of them, but still have some questions:

    1. Are there any method (in python of course) to redirect to a web page
    without causing a "Back" button trap(ie, when user click the back
    button on their web browser, they are redirect to their current page,
    while their hope is probably to go back to the last page they have
    seen, rather than the redirection page with a "Location: url" head and
    blank content.)?

    2. Are there any method to use relative path, rather than full absolute
    URI path in "Location: url"? It is very essential for later transplant
    work, e.g.,transplant a folder of cgi scripts from one web server to
    another, with different URL.

    Thank you.
    Sullivan WxPyQtKinter, Mar 27, 2006
    1. Advertisements

  2. Sullivan WxPyQtKinter enlightened us with:
    I don't know if this is possible using CGI. I use CherryPy, and it has
    an InternalRedirect method. Same is true for mod_python and Apache.

    Sybren Stuvel, Mar 27, 2006
    1. Advertisements

  3. I suspect the desired function may be browser specific, since it
    sounds like one would need to "pop" a history record to remove the
    "redirect" page from the list...
    Dennis Lee Bieber, Mar 27, 2006
  4. Sullivan WxPyQtKinter

    I V Guest

    I guess this may vary from browser to browser, but on Mozilla at least,
    if your CGI script returns one of the 300 status codes, then the
    redirect page doesn't get added to the browser history, and so the back
    button works as expected.
    You don't have to use absolute URLs in a Location: header returned by a
    CGI script. The web server will handle relative URLs for you. See the
    CGI spec:
    I V, Mar 27, 2006
  5. Dennis Lee Bieber enlightened us with:
    That's only if you think from the browser's point of view. An internal
    redirect goes unnoticed by the browser, hence there is no need for
    such history manipulation. With an internal redirect there is no
    "redirect page" at all, since the new URL is fetched from within the
    server, and sent to the browser as the response to the requested URL.

    Sybren Stuvel, Mar 28, 2006
  6. Sullivan WxPyQtKinter

    and-google Guest

    What's wrong with the redirection page?

    If there's really a necessary reason for not using an HTTP redirect
    (for example, needing to set a cookie, which doesn't work cross-browser
    Just read the name of the server (os.environ['SERVER_NAME']) to work
    out what absolute URL to redirect to, whist still being portable.

    Here's some code I dug up that should also cope with non-default ports
    and SSL, if that's of any use:

    ssl= os.environ.get('HTTPS', 'off') not in ('', 'off', 'false', 'no')
    scheme= ['http', 'https'][ssl]
    port= ['80', '443'][ssl]
    host= os.environ.get('SERVER_NAME', 'localhost')
    url= '%s://%s:%s' % (scheme, host, os.environ.get('SERVER_PORT',
    if url.endswith(':'+port):
    server= server[:-(len(port)+1)]
    url+= path

    (You *can* pass relative URLs back to the web server in a Location:
    header, but this should do an internal redirect inside the server,
    which may not be what you want.)
    and-google, Mar 28, 2006

  7. Sorry I do not quite understand what is the difference between an
    internal redirection and an external one?
    Sullivan WxPyQtKinter, Mar 28, 2006
  8. enlightened us with:
    That's a very ugly hack. For starters, it requires much more traffic
    than an internal redirect. Second, the URL changes, which might not be
    wanted. Third, it requires JavaScript for something that can be done
    without it.

    Sybren Stuvel, Mar 28, 2006
  9. Sullivan WxPyQtKinter enlightened us with:
    - Browser requests URL A
    - Server responds "Go to URL B"
    - Browser requests URL B
    - Server responds with contents of B
    - Browser displays B

    - Browser requests URL A
    - Server responds with contents of B
    - Browser displays B

    Sybren Stuvel, Mar 28, 2006
  10. Well, in that case, the internal direction is just what I need. Thank
    you so much for help.
    Sullivan WxPyQtKinter, Mar 29, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.