如何将 .htaccess 文件转换为 Firebase 托管配置?
How do I translate a .htaccess file to Firebase hosting config?
我在 Angular 2 中构建了一个 SPA,并将其托管在 Firebase 托管上。
我已经构建了一些专门用于抓取机器人的额外静态 html 页面(因为它们不读取更新的动态 html,仅读取初始 index.html),现在我需要重写 URL 用于从机器人到这些静态页面的 HTTP 请求。
我知道如何在 .htaccess
文件中执行此操作,但我不知道如何在我的 firebase.json
文件中转换重写条件。
这是我的 .htaccess
:
RewriteEngine On
# Remove trailing /
RewriteRule ^(.*)/$ / [L,R=301]
# Rewrite spiders to static html
RewriteCond %{HTTP_USER_AGENT} (googlebot|bingbot|msnbot|yahoo|Baidu|aolbuild|facebookexternalhit|iaskspider|DuckDuckBot|Applebot|Almaden|iarchive|archive.org_bot) [NC]
RewriteCond %{DOCUMENT_ROOT}/static%{REQUEST_URI}.html -f
RewriteRule ^(.*)$ /static/.html [L]
# Rewrite spiders to static index.html
RewriteCond %{HTTP_USER_AGENT} (googlebot|bingbot|msnbot|yahoo|Baidu|aolbuild|facebookexternalhit|iaskspider|DuckDuckBot|Applebot|Almaden|iarchive|archive.org_bot) [NC]
RewriteCond %{REQUEST_URI} "^/$"
RewriteRule ^ /static/index.html [L]
# If an existing asset or directory is requested, serve it
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use the Angular app entry point
RewriteRule ^ /index.html
我在 Configuring Rewrites 上阅读了 Firebase 的文档,但无法弄清楚如何定位特定的用户代理。
有什么想法吗?
Firebase 托管不支持基于 user-agent header 配置重写。可以支持基于路径的重写,rewrites based on the language of the user/browser.
我知道的基于其他 header 重写的唯一选择是 connect Firebase Hosting to Cloud Functions or Cloud Run 并在代码中重写。但这与在 firebase.json
文件中配置重写有很大不同,因此我建议在选择此路径之前阅读它。
我在 Angular 2 中构建了一个 SPA,并将其托管在 Firebase 托管上。 我已经构建了一些专门用于抓取机器人的额外静态 html 页面(因为它们不读取更新的动态 html,仅读取初始 index.html),现在我需要重写 URL 用于从机器人到这些静态页面的 HTTP 请求。
我知道如何在 .htaccess
文件中执行此操作,但我不知道如何在我的 firebase.json
文件中转换重写条件。
这是我的 .htaccess
:
RewriteEngine On
# Remove trailing /
RewriteRule ^(.*)/$ / [L,R=301]
# Rewrite spiders to static html
RewriteCond %{HTTP_USER_AGENT} (googlebot|bingbot|msnbot|yahoo|Baidu|aolbuild|facebookexternalhit|iaskspider|DuckDuckBot|Applebot|Almaden|iarchive|archive.org_bot) [NC]
RewriteCond %{DOCUMENT_ROOT}/static%{REQUEST_URI}.html -f
RewriteRule ^(.*)$ /static/.html [L]
# Rewrite spiders to static index.html
RewriteCond %{HTTP_USER_AGENT} (googlebot|bingbot|msnbot|yahoo|Baidu|aolbuild|facebookexternalhit|iaskspider|DuckDuckBot|Applebot|Almaden|iarchive|archive.org_bot) [NC]
RewriteCond %{REQUEST_URI} "^/$"
RewriteRule ^ /static/index.html [L]
# If an existing asset or directory is requested, serve it
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use the Angular app entry point
RewriteRule ^ /index.html
我在 Configuring Rewrites 上阅读了 Firebase 的文档,但无法弄清楚如何定位特定的用户代理。
有什么想法吗?
Firebase 托管不支持基于 user-agent header 配置重写。可以支持基于路径的重写,rewrites based on the language of the user/browser.
我知道的基于其他 header 重写的唯一选择是 connect Firebase Hosting to Cloud Functions or Cloud Run 并在代码中重写。但这与在 firebase.json
文件中配置重写有很大不同,因此我建议在选择此路径之前阅读它。