[mod_python] redirects and url rewriting

Daniel J. Popowich dpopowich at mtrsd.k12.ma.us
Mon Dec 22 11:44:30 EST 2003


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



More information about the Mod_python mailing list