GAE Application app.yaml VS .htaccess

后端 未结 2 1911
我寻月下人不归
我寻月下人不归 2020-12-07 06:37

How to Write a app yaml looks like htacess below

    RewriteEngine on
    # To append a query string part in the substitution string
    RewriteRule ^([0-9a-         


        
相关标签:
2条回答
  • 2020-12-07 07:15

    The GAE app.yaml doesn't have a URL rewrite capability, it just parses the incoming request URL for request routing purposes, to determine which handlers to invoke.

    One could maybe argue that the static_file handlers configuration has a somewhat similar capability, but it is only applicable to the static assets.

    For the dynamic handlers you'd need to take care of such "rewrite" inside your app code. I'm quoting "rewrite" here as technically it's just a different way of parsing/interpreting the request URL inside your app code - the original, unchanged request URL will still be the one recorded by the GAE infra.

    0 讨论(0)
  • 2020-12-07 07:16

    as Dan mentioned, you will not be able to handle this all in the yaml, and will need to to handle the logic yourself, we do a simular thing in one of our project and will outline below our solution.

    Our scenario is handling the old website article's URL structure, and trying to redirect them to the new URL structure.

    In our yaml we register the pattern that we are looking to match on and direct it to a file where we will do the handling :

    - url: (/.*/[0-9]{4}/[0-9]{2}/[0-9]{2}/.*)  (Pattern to match on)
      script: publication.custom.redirector.app (Path to your .py that will have your handling in)
    

    In our .py file we will catch that pattern and route it to our DefaultHandler that can then do any logic you need and redirect out: ( in our project this goes to /publication/custom/redirector.py )

    import request
    import settings
    import re
    
    class DefaultHandler(request.handler):
    
        def get(self, pre, year, month, day, post):
    
            post = re.sub('(.*[^0-9])[\d]{1}$', r'\1', post)
            post = re.sub('[^0-9a-zA-Z-_\/]+', '', post)
            path = post.split("/")[-1]
            slug = "{0}-{1}-{2}-{3}".format(year, month, day, path)
    
            article = self.context.call('pub/articles/get', slug=slug.lower())
            if article:
                self.redirect(article['pub_url'], permanent=True)
            else:
                self.render("pages/page-not-found/page-not-found.html")
    
    
    app = request.app([
        ('/(.*)/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)', DefaultHandler)
    ], settings.gaext.config)
    

    Hope this helps

    0 讨论(0)
提交回复
热议问题