【问题标题】:Google Cloud App Engine redirect all /*.html endings to /?Google Cloud App Engine 将所有 /*.html 结尾重定向到 /?
【发布时间】:2021-05-17 06:49:48
【问题描述】:

我在互联网上有很多包含 .html 结尾的反向链接。例如。 https://mywebsite.de/blog.html 但与此同时,我所有的网站都以https://mywebsite.de/blog 的身份提供服务。 我使用 Flask 后端,但没有找到将这些 .html 结尾重定向到 / 的解决方案。 是否有可能为此使用 app.yaml 配置文件? 可能是这样的:

handlers:
- url: /*.html
{define here the redirect}

我发现文档中对此没有任何帮助。 https://cloud.google.com/appengine/docs/standard/python/config/appref?hl=de#handlers_element

【问题讨论】:

    标签: google-app-engine flask redirect url-rewriting app.yaml


    【解决方案1】:

    正如 gaefan 建议的那样,我用 Flask 本身解决了这个问题。我的解决方案如下所示:

    @app.route('/blog/<slug>.html')
    def html_redirect(slug):
        return redirect(f"https://mywebsite.de/blog/{slug}" ,code=301)
    

    注意:这只重定向到 /blog 的子站点。不仅仅是包含“.html”结尾的任何内容。

    【讨论】:

      【解决方案2】:

      您需要在 Flask 中执行此操作。我看到两个主要选项。

      (1) 您可以使用正则表达式创建匹配的 Flask 路由。然后匹配所有以.html 结尾的路由并进行重定向。

      (2) 在函数上使用@app.before_request,检查request.url是否以.html结尾,然后重定向。

      请注意,Flask 中的 redirect 默认为 302,您可能希望指定 301 重定向。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-22
        • 2010-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多