【发布时间】: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