CGI redirection: let us discuss it further

and-google at doxdesk.com and-google at doxdesk.com
Tue Mar 28 12:21:46 EST 2006


Sullivan WxPyQtKinter wrote:

> 1. Are there any method (in python of course) to redirect to a web page
> without causing a "Back" button trap... rather than the redirection page
> with a "Location: url" head

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
on redirects), the best bet is a page containing a plain link and
<script>-redirect, using location.replace() to avoid the back button
trap.

> 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.

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',
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 Clover
mailto:and at doxdesk.com
http://www.doxdesk.com/




More information about the Python-list mailing list