2013-02-04 39 views
0

我在google appengine上用python託管websiete。我有單獨的頁面,在目錄中,我需要知道如何從eg.:www.example.com/page重定向。如果我不在頁面後添加另一個「/」,則會出現「404未找到」。如何更改我的重定向腳本以添加此?這裏是我的app.yaml:Html 301在google appengine中重定向

application: example-app 
    version: 4 
    runtime: python 
    api_version: 1 

    handlers: 
    - url: (.*)/ 
     static_files: static\1/index.html 
     upload: static/index.html 

    - url:/
     static_dir: static 



    - url: /apps/(.*\.apk) 
     mime_type: application/vnd.android.package-archive 
     static_files: static/apps/\1 
     upload: static/apps/(.*\.apk) 

    - url: (.*)/apps 
     script: directory.py 

    - url: /comingsoon 
     script: DirecotryHandler.py 

    - url: /directory 
     script: DirecotryHandler.py 

,這裏是我的directoryhandler.py:

import os 
    from google.appengine.ext import webapp 
    from google.appengine.ext.webapp.util import run_wsgi_app 

    class DirecotryHandler(webapp.RequestHandler): 
    def get(self, tail): 
    if tail == î: 
    self.redirect(ë/directory/í, permanent=True) 
    #TODO: respond to ì/directory/î request 

    def head(self, tail): 
    if tail == î: 
    self.redirect(ë/directory/í, permanent=True) 
    #TODO: respond to ì/directory/î request 

    application = webapp.WSGIApplication(
    [ 
    (r'^/directory(/?)$', DirectoryPage), 
    ]) 

    def main(): 
    run_wsgi_app(application) 

    if __name__ == ì__main__î: 
    main() 

如何編輯我DirectryHandler.py處理301個重定向?先謝謝您的幫助!

回答

0

實際上,它是處理301重定向的瀏覽器。您的代碼只是缺少路線(r'^/comingsoon$', DirecotryHandler),以生成301重定向。

然後,當您的瀏覽器調用www.example.com/comingsoon時,您的DirecotryHandler類處理請求並調用self.redirect("/directory/", permanent=True)。這會生成301重定向作爲對瀏覽器的響應,該瀏覽器會看到新的URL /directory/並請求它,這會調用您的DirectoryPage類。