[mod_python] redirects and url rewriting

Daniel J. Popowich dpopowich at mtrsd.k12.ma.us
Mon Dec 22 12:34:09 EST 2003



> I think the best option for your problem is:
> 
> req.headers_out[ "Location" ] = uri
> return apache.HTTP_MOVED_PERMANENTLY #301 
> 

This won't work for some of what I'm doing, i.e., user goes to a page
that needs authentication so they are redirected to a login page
(without having to save state to a session).  There are other similar
situations where, programmatically, you discover that the user should
be somewhere else within your mod_python app and you'd like to
redirect them to other URIs which can check req.prev for existing
state (thus saving network traffic, server hits, etc.  Heck, you got
everything you need to know, why tell the client, "go here" when you
can just deliver it in the first place?).

Also, being the kind of geek that needs to know how and why things
work, I'd like to know why some redirects rewrite my browser url and
some don't; ultimately, I'd like to be able to control the behaviour
with mod_python.

Dan

> >
> >When I use req.internal_redirect(some_uri) sometimes the url in my
> >browser gets rewritten to reflect some_uri and some times it doesn't,
> >remaining as the uri of the originally selected page.  This can cause
> >numerous problems with relative links inside a page if the url is not
> >rewritten in the browser.
> >
> >I'm trying to understand what causes this and how I can control it.
> >Can someone point me to some doc?
> >
> >Below is a short handler I wrote as an example (the links I use assume
> >you have the apache manual installed; modify if necessary).
> >
> >[modpython 3.1.2b, python 2.3.2, apache 2.0.40]
> >
> >Thanks,
> >
> >------------------
> >Daniel Popowich
> >Network Specialist
> >-------------------------------------
> >Mohawk Trail Regional School District
> >24 Ashfield Rd.
> >Shelburne Falls, MA 01370
> >413.625.0192 x22
> >-------------------------------------
> >
> ># -*- python -*-
> >
> >from mod_python import apache, util
> >
> >def handler(req):
> >
> >    req.content_type = 'text/html'
> >
> >    form = util.FieldStorage(req)
> >    choice = form.getfirst('redirect')
> >    # this doesn't rewrite the url in the browser
> >    if choice == '1':
> >        req.internal_redirect('%s/abcdefg' % req.uri)
> >        
> >    # neither does this one [and the resulting page is quite
> >    # messed up because the relative urls inside the doc don't
> >    # make sense without the rewrite]
> >    elif choice == '2':
> >        req.internal_redirect('/manual/index.html')
> >
> >    # but this one does
> >    elif choice == '3':
> >        req.internal_redirect('/manual')
> >
> >    else:
> >        req.write('''
> >        <html>
> >        <body>
> >        <a href=%s?redirect=1>redirect to %s/abcdefg</a><br>
> >        <a href=%s?redirect=2>redirect to /manual/index.html</a><br>
> >        <a href=%s?redirect=3>redirect to /manual</a><br>
> >        </body>
> >        </html>
> >        ''' % ((req.uri,)*4))
> >
> >
> >    return apache.OK
> >
> >_______________________________________________
> >Mod_python mailing list
> >Mod_python at modpython.org
> >http://mailman.modpython.org/mailman/listinfo/mod_python
> >
> >



More information about the Mod_python mailing list