apache - .htaccess 规则将爬虫重定向到另一个文件夹
问题描述
我有一个单页应用程序(react、react-router),我正在尝试将搜索和共享机器人/爬虫重定向到具有应用程序静态快照的不同文件夹。我有一个 .htaccess 设置,它大部分都在工作,但缺少一些东西。
- 它当前正确地重定向
domain.com/something
到domain.com/snap/something/
- 编辑:它现在正确重定向
domain.com
到domain.com/snap/
- 它不处理
domain.com/non-existing
,它应该重定向到domain.com/snap/404.html
但当前重定向到domain.com/snap/non-existing
我拥有的 .htaccess 是
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
# For crawlers show snapshots
RewriteCond %{HTTP_USER_AGENT} googlebot|bingbot|yandex|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
RewriteCond %{REQUEST_URI} !^/(snap) [NC]
# Proxy the request
RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent|\.ttf|\.woff))(.*) /snap/$2/ [R=301,L]
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# If the requested pattern is file and file doesn't exist, send 404
RewriteCond %{REQUEST_URI} ^(\/[a-z_\-\s0-9\.]+)+\.[a-zA-Z]{2,4}$
RewriteRule ^ - [L,R=404]
# otherwise use history router
RewriteRule ^ /index.html [L]
</IfModule>
编辑:我将 .htaccess 的爬虫部分向上移动,现在它可以正确处理domain.com -> domain.com/snap/
场景,但我不知道如何让它处理不存在的路线。
解决方案
要将您的 uri 重定向404
到正确的目的地,您可以使用以下规则。把它放在你的www to non-www
重定向规则块下面,并确保在测试之前清除你的浏览器缓存:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_USER_AGENT} googlebot|bingbot|yandex|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
RewriteCond %{REQUEST_URI} !^/(snap) [NC]
# Proxy the request
RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent|\.ttf|\.woff))(.*) /snap/404.html [R=301,L]
推荐阅读
- powershell - 根据列的值将 CSV 文件拆分为少量 csv 文件
- javascript - 创建动态按钮时javascript重新加载addEventListner
- java - 使用 gson 将 Java 对象转换为 JSON 字符串
- java - 为什么将 C# HTTP 请求转换为 Java 不起作用?
- gradle - 极慢的 Gradle 复制任务
- ruby-on-rails - Rails 5:rails url 错误的自定义 slug
- android - 动态改变transformCenter
- c# - 无法从排行榜加载分数
- twitter-bootstrap - Bootstrap 可点击可排序 - 排序后无法点击
- python - 如何修复“UnboundLocalError:分配前引用的局部变量‘books’”?