【问题标题】:Html 301 redirect in google appengine谷歌应用引擎中的 Html 301 重定向
【发布时间】:2013-01-19 05:37:46
【问题描述】:

我正在使用 python 在 google appengine 上托管一个网站。我在目录中有单独的页面,我需要知道如何从例如:“www.example.com/page”重定向。我没有在页面后添加另一个“/”,它会出现“404 not found”。如何更改我的重定向脚本以添加它?这是我的 app.yaml:

    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 重定向?提前感谢您的帮助!

【问题讨论】:

    标签: python html google-app-engine redirect


    【解决方案1】:

    实际上,处理 301 重定向的是您的浏览器。您的代码只是缺少路由 (r'^/comingsoon$', DirecotryHandler), 来生成 301 重定向。

    然后,当您的浏览器调用www.example.com/comingsoon 时,您的 DirecotryHandler 类会处理该请求并调用self.redirect("/directory/", permanent=True)。这会生成 301 重定向作为对您的浏览器的响应,浏览器会看到新的 URL /directory/ 并请求它,它会调用您的 DirectoryPage 类。

    【讨论】:

      猜你喜欢
      • 2013-09-25
      • 1970-01-01
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      • 1970-01-01
      • 2018-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多