2012-12-30 33 views
3

我寫了一個簡單的python腳本來重定向一次網址,一旦廣告被點擊。
與廣告相關聯的網址是http://server.example.com/redirect.py剛剛打印到屏幕的重定向url - python

redirect.py的代碼如下所示:

import cgi 
import cgitb 

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

然而,代替被點擊該廣告時將請求重定向到www.finalurl.com,最終URL被簡單地顯示在瀏覽器窗口中。所有幫助非常感謝。

請注意,redirect.py腳本位於/public_html/cgi-bin/文件夾中,腳本具有711權限。此外,目標網址並不總是相同的網址,它基於數據庫查詢。該代碼簡單省略。

我也嘗試包括:print ("Status: 301 Moved")

+0

請選擇答案,如果你認爲這個解決您的問題接受。 – Nilesh

回答

4

「\ n \ n」是HTTP報頭標記的結束,請使用來代替:

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 
+0

完美的作品!謝謝你的幫助! – jordanskis