我们的网站基于Angular,它几乎完全基于JavaScript,因此我们需要为Googlebot提供静态HTML快照,以便抓取我们。 目前,我们有这个实施:
location / { # Rewrite rules for the Google and Bing bots and other crawlers. # Serves static HTML from /snapshots when a URL is requested if ($http_user_agent ~ (Googlebot|bingbot|Screaming)) { rewrite ^/(.*)$ /snapshots/$1.html break; } }
这在大多数情况下都能正常工作,但是如果Google要求一个URL,例如: http://site.com/support/contact/ : http://site.com/support/contact/ ,它将被重写成: http://site.com/support/contact/.html : http://site.com/support/contact/.html显然会返回一个404。我需要更改configuration以在URL末尾删除正斜杠,以确保返回: http://site.com/support/contact.html : http://site.com/support/contact.html
这怎么能在nginxconfiguration中实现呢? 因此,我们在网站站长工具中看到了数百个404错误。
谢谢!
如果你改变你的重写
rewrite ^/(.*)/$ /snapshots/$1.html break; rewrite ^/(.*)$ /snapshots/$1.html break;
那么第一行只会匹配以斜线结尾的行,而$1将包含完整path减去前导斜杠和尾随斜杠。 第二个将抓住其余的案件(现在正在工作)。