在 Google Cloud App Engine 上,你可以使用 app.yaml
文件来配置 URL 重定向。为了将所有以 .html
结尾的请求重定向到根路径 /
,你可以使用 handlers
和 redirect
配置。
以下是一个示例 app.yaml
文件,展示了如何实现这个重定向:
runtime: python39 # 或者你使用的其他运行时
handlers:
- url: /.*\.html
script: auto
secure: always
redirect_http_response_code: 301
static_files: static/index.html
upload: static/index.html
- url: /.*
script: auto
secure: always
在这个配置中:
url: /.*\.html
匹配所有以 .html
结尾的 URL。script: auto
表示自动处理请求。secure: always
强制使用 HTTPS。redirect_http_response_code: 301
设置重定向的 HTTP 响应代码为 301(永久重定向)。static_files: static/index.html
和 upload: static/index.html
用于将所有匹配的请求重定向到 index.html
文件。url: /.*\.html
:这个正则表达式匹配所有以 .html
结尾的 URL。script: auto
:表示自动处理请求。secure: always
:强制使用 HTTPS。redirect_http_response_code: 301
:设置重定向的 HTTP 响应代码为 301(永久重定向)。static_files: static/index.html
和 upload: static/index.html
:将所有匹配的请求重定向到 index.html
文件。index.html
文件存在于指定的路径中。runtime
字段。假设你的项目结构如下:
my-app/
├── app.yaml
├── static/
│ └── index.html
└── main.py
app.yaml
文件内容:
runtime: python39
handlers:
- url: /.*\.html
script: auto
secure: always
redirect_http_response_code: 301
static_files: static/index.html
upload: static/index.html
- url: /.*
script: auto
secure: always
static/index.html
文件内容:
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Welcome to the Home Page</h1>
</body>
</html>
main.py
文件内容:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
通过这种方式,你可以将所有以 .html
结尾的请求重定向到根路径 /
,并且确保你的应用程序在 Google Cloud App Engine 上正确运行。
领取专属 10元无门槛券
手把手带您无忧上云