.htaccess规则将爬网程序重定向到另一个文件夹

时间:2018-11-28 08:34:50

标签: apache .htaccess redirect mod-rewrite

我有一个单页应用程序(反应,反应路由器),并且我尝试使用该应用程序的静态快照重定向搜索并将机器人/爬网程序共享到另一个文件夹。我有一个.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/场景,但是我不知道如何使它处理不存在的路由。

1 个答案:

答案 0 :(得分:0)

要将404 uri重定向到正确的目的地,可以使用以下规则。将这个放在您的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]