.htaccess重定向进入无限循环

时间:2016-11-15 05:33:41

标签: php angularjs apache .htaccess redirect

我正在使用Angular JS,而SEO用于将所有抓取请求重定向到静态html页面内容。

我正在使用Apache服务器,所以在我写的.htaccess文件中:

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} Googlebot [NC,OR]
RewriteCond %{HTTP_USER_AGENT} facebookexternalhit [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} Twitterbot [NC,OR]
RewriteCond %{HTTP_USER_AGENT} Baiduspider [NC,OR]
RewriteCond %{HTTP_USER_AGENT} MetaURI [NC,OR]
RewriteCond %{HTTP_USER_AGENT} mediawords [NC,OR]
RewriteCond %{HTTP_USER_AGENT} FlipboardProxy [NC,OR]
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*?)\/*$
RewriteRule ^(.*)$ snapshots/$1.html [R=301,L]

但它转向无限重定向。

请转到此处:http://www.redirect-checker.org/index.php并输入链接为:http://templatic.net/test/job_category/mobile并选择搜索引擎为Googlebot,以便将其重定向到:

enter image description here

所以请帮我阻止这个...

1 个答案:

答案 0 :(得分:0)

一旦RewriteCond匹配,请求test/job_category/mobile被重写为test/snapshots/job_category/mobile.html,重写的请求将被传回URL解析引擎,规则集将从一开始就再次运行

这一次,它会再次与HTTP_USER_AGENT匹配并被重写为test/snapshots/snapshots/job_category/mobile.html.html,然后再次发回,然后再重写等等......

修复是在您的RewriteRule中,如果请求已经以snapshots/开头,则阻止重写:

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} Googlebot [NC,OR]
RewriteCond %{HTTP_USER_AGENT} facebookexternalhit [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} Twitterbot [NC,OR]
RewriteCond %{HTTP_USER_AGENT} Baiduspider [NC,OR]
RewriteCond %{HTTP_USER_AGENT} MetaURI [NC,OR]
RewriteCond %{HTTP_USER_AGENT} mediawords [NC,OR]
RewriteCond %{HTTP_USER_AGENT} FlipboardProxy [NC,OR]
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*?)\/*$
RewriteRule ^(?!snapshots/)(.*)$ snapshots/$1.html [R=301,L]