我正在使用python在google appengine上主持一个websiete。我在目录中有单独的页面,我需要知道如何从例如:“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重定向?在此先感谢您的帮助!
答案 0 :(得分:0)
实际上,这是您的浏览器处理301重定向。您的代码只是错过了路由(r'^/comingsoon$', DirecotryHandler),
来生成301重定向。
然后,当您的浏览器调用www.example.com/comingsoon
时,您的DirecotryHandler类会处理请求并调用self.redirect("/directory/", permanent=True)
。这会生成301重定向作为对您的浏览器的响应,该浏览器会看到新的网址/directory/
并请求它,这会调用您的DirectoryPage
类。