CGI redirection: let us discuss it further

  • Thread starter Sullivan WxPyQtKinter
  • Start date
S

Sullivan WxPyQtKinter

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

Sybren Stuvel

Sullivan WxPyQtKinter enlightened us with:
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.)?

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
 
D

Dennis Lee Bieber

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

I V

Sullivan said:
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.)?

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

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:

http://hoohoo.ncsa.uiuc.edu/cgi/out.html
 
S

Sybren Stuvel

Dennis Lee Bieber enlightened us with:
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...

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
 
A

and-google

Sullivan said:
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
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.)
 
S

Sullivan WxPyQtKinter

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


Sorry I do not quite understand what is the difference between an
internal redirection and an external one?
 
S

Sybren Stuvel

(e-mail address removed) enlightened us with:
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.

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
 
S

Sybren Stuvel

Sullivan WxPyQtKinter enlightened us with:
Sorry I do not quite understand what is the difference between an
internal redirection and an external one?

External:
- Browser requests URL A
- Server responds "Go to URL B"
- Browser requests URL B
- Server responds with contents of B
- Browser displays B

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

Sybren
 
S

Sullivan WxPyQtKinter

Well, in that case, the internal direction is just what I need. Thank
you so much for help.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top