21

すべてのURLを別のURLにリダイレクトするようにapp.yamlファイルを構成するにはどうすればよいですか?たとえば、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
4

7 に答える 7

40

Webapp2にはリダイレクトハンドラが組み込まれています

独自のハンドラーをロールする必要はありません。webapp2にはすでに1つ付属しています。

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)

_uri引数は、RedirectHandlerクラスが宛先を定義するために使用するものです。これに関するドキュメントを見つけるのに多くのGoogleFuが必要でしたが、私のアプリでは問題なく動作します。

アップデート:

私はあなたがこれを知っていると仮定しました、しかしあなたはあなたのキャッチオールルートを以下から変える必要があります:

- url: /
  static_dir: static

宛先(python27バージョン):

- url: /.*
  script: main.application

または:(python27より前のバージョン)

- url: /.*
  script: main.py

main.pyは、リクエストハンドラーとルートを含むファイルです。

注:静的ファイルの性質上、GAEでリダイレクトを処理する静的のみの方法はありません。基本的に、app.yamlだけでリダイレクトを行う方法はありません。

于 2012-02-24T09:24:06.077 に答える
9

必要なものすべて(replace app-idhttp://example.com):

  • app.yaml

    application: app-id
    version: 1
    runtime: python27
    api_version: 1
    threadsafe: false
    
    handlers:
    - url: /.*
      script: main.py
    
  • 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()
    
于 2013-08-18T09:42:36.773 に答える
5

Pythonハンドラーを使用すると、すべてのリクエストを簡単にリダイレクトできます。何かのようなもの

class FormHandler(webapp.RequestHandler):
  def post(self):
    if processFormData(self.request):
      self.redirect("http://domain.com")
于 2009-06-29T15:09:23.047 に答える
4

リダイレクトを行うPythonスクリプトは次のとおりです。

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

class MainPage(webapp.RequestHandler):
  def get(self, path):
    self.redirect("http://example.com", permanent=True)
  def head(self, path):
    self.redirect("http://example.com", permanent=True)

application = webapp.WSGIApplication(
            [
                (r'^(.*)', MainPage)
            ])

def main():
   run_wsgi_app(application)

if __name__ == "__main__":
    main()
于 2012-05-01T13:30:21.430 に答える
4

「リダイレクト」を行う静的ファイルのみの方法が必要な場合は、次のようにします。

app.yamlで、これをキャッチオールとしてファイルの最後に配置します。

-   url: /.*
    static_files: root/redirect.html
    upload: root/redirect.html

次に、root/redirect.htmlファイルに次のように入力します。

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="refresh" content="0;URL=/" />
        <script>
            location.replace("/");
        </script>
    </head>
    <body></body>
</html>

この例では、すべての不明なURLをルート(つまり、/)にリダイレクトします。別のURLが必要な場合は、適切な場所でhttp://mydomain.comに置き換えてください。

于 2014-06-06T19:27:28.717 に答える
0

Evanの答えをリフして、すべてのリクエストをリダイレクトするには、正規表現を使用して次のようなことを行うことができます。

import webapp2
from webapp2_extras.routes import RedirectRoute

app = webapp2.WSGIApplication([
     RedirectRoute('/<:.*>', redirect_to='/')
    ], debug=False)

公式ドキュメントについては、以下を確認してください。

https://webapp2.readthedocs.io/en/latest/guide/routing.html

https://webapp2.readthedocs.io/en/latest/api/webapp2.html#webapp2.Route。初期化

于 2015-09-03T09:52:25.090 に答える
0

前の回答で説明したwebapp2のリダイレクトハンドラー(webapp2.RedirectHandler)は、postメソッドが含まれていないため、postリクエストでは機能しません(https://github.com/GoogleCloudPlatform/webapp2/blob/master/webapp2を参照)。 py)なので、投稿に関心がある場合は、独自のpythonハンドラーをロールする必要があります。次のようなもの:

import webapp2

class MainPage(webapp2.RequestHandler):


    def post(self):
        self.redirect('http://example.com')


application = webapp2.WSGIApplication([
('/.*', MainPage)
], debug=False)
于 2017-09-09T12:37:56.620 に答える