Warm tip: This article is reproduced from serverfault.com, please click

url rewriting-如何将.htaccess文件转换为Firebase托管配置?

(url rewriting - How do I translate a .htaccess file to Firebase hosting config?)

发布于 2020-11-29 14:26:46

我已经在Angular 2中建立了SPA,并将其托管在Firebase Hosting上。我专门为爬网机器人构建了一些额外的静态html页面(因为它们不读取更新的动态html,仅读取初始index.html),现在我需要将来自机器人的HTTP请求的URL重写到这些静态页面。

我知道如何在.htaccess文件中执行此操作,但是我不知道如何转换firebase.json文件中的重写条件

这是我的.htaccess

RewriteEngine On
 
# Remove trailing /
RewriteRule ^(.*)/$ /$1 [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/$1.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

我已经阅读了Firebase关于“配置重写”的文档,但无法弄清楚如何以特定的用户代理为目标。

有任何想法吗?

Questioner
Alex
Viewed
12
Frank van Puffelen 2020-11-29 22:45:09

Firebase托管不支持基于用户代理标头配置重写。它可以支持基于路径的重写,也可以基于用户/浏览器的语言进行重写

我知道要基于其他标头进行重写的唯一选择是将Firebase Hosting连接到Cloud Functions或Cloud Run并用代码进行重写。但这与在firebase.json文件中配置重写大不相同,因此我建议在选择此路径之前先对其进行阅读。