【问题标题】:How do I translate a .htaccess file to Firebase hosting config?如何将 .htaccess 文件转换为 Firebase 托管配置?
【发布时间】: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

我在 Configuring Rewrites 上阅读了 Firebase 的文档,但不知道如何定位特定的用户代理。

有什么想法吗?

【问题讨论】:

    标签: .htaccess url-rewriting firebase-hosting


    【解决方案1】:

    Firebase 托管不支持基于用户代理标头配置重写。它可以支持基于路径的重写,以及rewrites based on the language of the user/browser

    我所知道的根据其他标头重写的唯一选择是 connect Firebase Hosting to Cloud Functions or Cloud Run 并在代码中进行重写。但这与在 firebase.json 文件中配置重写是一个截然不同的练习,因此我建议在选择此路径之前阅读它。

    【讨论】:

    • 我只是想看看这是否有变化,试图找到更好的社交分享解决方案,以便当 Facebook 或 twitter 尝试读取 url 时
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2019-02-08
    • 2021-10-30
    • 2015-05-25
    • 2021-10-21
    • 1970-01-01
    相关资源
    最近更新 更多