我刚刚用 create-react-app 写了一篇博客。除此之外,我还编写了一个小型服务,该服务将在服务器上使用 chrome 为 Google 或 Facebook 等爬虫呈现网站。
所以我有 http://example.com/path-to-page
应该提供给普通用户,还有 http://myprerenderer.com/render/http ://example.com/path-to-page
应提供给爬虫等。
这是我的 .htacces 目前的样子:
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
# redirect non www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
# One year for image files
<filesMatch ".(jpg|jpeg|png|gif|ico|svg)$">
Header set Cache-Control "max-age=31536000, public"
</filesMatch>
# One month for css and js
<filesMatch ".(css|js)$">
Header set Cache-Control "max-age=2628000, public"
</filesMatch>
这适用于我访问的每个 url 都由 react 应用程序 index.html 提供服务(顺便说一句,我正在使用 react 路由器。)。
现在,我需要如何针对这种情况调整我的 .htacces: 爬虫访问该网站,被重定向(或代理?)到预呈现器,然后预呈现器使用 chrome 为网站提供服务 - 但如果该网站已经被预呈现服务访问过,则不应再进行重定向。
谢谢!
最佳答案
您可能想要添加类似于 Prerender.io .htaccess 的内容来检测爬虫和代理(而不是重定向)对您的预呈现服务器的请求:
关于reactjs - .htaccess 机器人代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49488466/