Search code examples
pythonurlhttp-redirectcgi-bin

Redirect url just printing to screen - python


I wrote a simple python script to redirect a url once an advertisement is clicked on.
The url associated with the advertisement is http://server.example.com/redirect.py.

The code for redirect.py is shown below:

import cgi
import cgitb

cgitb.enable()
print('Content-Type: text/html\n\n')
print('Location: http://www.finalurl.com')

However, instead of redirecting the request to www.finalurl.com when the advertisement is clicked on, the final url is simply displayed in the browser window. All help is GREATLY appreciated.

Note that the redirect.py script is in the /public_html/cgi-bin/ folder and the script has 711 permissions. Also that the destination url will not always be the same url, it is based on a database query. That code was simple omitted.

I also tried including: print ("Status: 301 Moved")


Solution

  • '\n\n' is end of http header marker, use instead:

    print 'Status: 301 Moved Permanently'
    print 'Location: http://www.finalurl.com'
    print 'Content-Type: text/html'
    print # end of header
    ... # short html with the url