mis*_*ero 19 google-app-engine yaml
如何配置app.yaml文件以将所有URL重定向到另一个URL?例如,我想http://test.appspot.com/hello或http://test.appspot.com/hello28928723重定向到http://domain.com.
我目前只提供静态文件.这是我的app.yaml文件:
application: testapp
version: 1
runtime: python
api_version: 1
handlers:
- url: (.*)/
static_files: static\1/index.html
upload: static/index.html
- url: /
static_dir: static
Run Code Online (Sandbox Code Playgroud)
Eva*_*ice 40
Webapp2有一个内置的重定向处理程序
无需滚动自己的处理程序; webapp2已经附带一个.
application = webapp2.WSGIApplication([
webapp2.Route('/hello', webapp2.RedirectHandler, defaults={'_uri':'http://domain.com'}),
webapp2.Route('/hello28928723', webapp2.RedirectHandler, defaults={'_uri':'http://domain.com'}),
], debug=False)
Run Code Online (Sandbox Code Playgroud)
_uri参数是RedirectHandler类用于定义目标的内容.需要很多Google Fu才能找到相关的文档,但它在我的应用程序中运行完美.
更新:
我以为你知道这一点,但是你需要改变你的所有路线:
- url: /
static_dir: static
Run Code Online (Sandbox Code Playgroud)
至(python27版本):
- url: /.*
script: main.application
Run Code Online (Sandbox Code Playgroud)
或者:( pre python27版本)
- url: /.*
script: main.py
Run Code Online (Sandbox Code Playgroud)
main.py是包含请求处理程序+路由的文件.
注意:由于静态文件的性质,在GAE上没有静态方法来处理重定向.基本上,单独在app.yaml中无法进行重定向.
所有你需要(替换app-id,http://example.com):
app.yaml:
application: app-id
version: 1
runtime: python27
api_version: 1
threadsafe: false
handlers:
- url: /.*
script: main.py
Run Code Online (Sandbox Code Playgroud)main.py:
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class AllHandler(webapp.RequestHandler):
def get(self):
self.redirect("http://example.com", True)
application = webapp.WSGIApplication([('/.*', AllHandler)])
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)您可以使用python处理程序轻松地重定向所有请求.就像是
class FormHandler(webapp.RequestHandler):
def post(self):
if processFormData(self.request):
self.redirect("http://domain.com")
Run Code Online (Sandbox Code Playgroud)
如果您想要仅静态文件的方式来执行“重定向”,请执行以下操作:
在 中app.yaml,将其作为包罗万象的内容放在文件末尾:
- url: /.*
static_files: root/redirect.html
upload: root/redirect.html
Run Code Online (Sandbox Code Playgroud)
然后在root/redirect.html文件中输入:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="0;URL=/" />
<script>
location.replace("/");
</script>
</head>
<body></body>
</html>
Run Code Online (Sandbox Code Playgroud)
此示例将所有未知 URL 重定向到根目录(即 / )。如果您想要另一个 URL,只需http://example.com在适当的位置进行替换即可。
| 归档时间: |
|
| 查看次数: |
20601 次 |
| 最近记录: |