Angular with Prerender(.htaccess设置)

时间:2016-12-12 11:10:14

标签: angularjs .htaccess prerender serverside-rendering

我正在尝试使用Prerender服务为抓取工具的服务器端渲染设置Angular 1.5应用。

一切都适用于内页,但主页的呈现存在问题 - 抓取工具会看到404页面而不是主页面。

我想我的.htaccess中有一些其他规则存在问题 - 除了Prerender的规则外,我对所有页面都使用了另外两条规则:

  • 使用尾部斜杠
  • 重写网址,而不会在网址上留下斜杠
  • 在没有www
  • 的网址上用www重写网址

欢迎任何提示!

这是Apache服务器的.htaccess文件

RequestHeader set X-Prerender-Token "MyToken"

  RewriteEngine On

  RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
  RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]


  # If an existing asset or directory is requested go to it as it is
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
  RewriteRule ^ - [L]

  RewriteCond %{REQUEST_URI}  ^/$
  RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
  RewriteRule ^(.*)$ /snapshots/%1? [NC,L]

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*[^/])$ /$1/ [L,R=301]

  <IfModule mod_proxy_http.c>
      RewriteCond %{HTTP_USER_AGENT} Googlebot|bingbot|Googlebot-Mobile|Baiduspider|Yahoo|YahooSeeker|DoCoMo|Twitterbot|TweetmemeBot|Twikle|Netseer|Daumoa|SeznamBot|Ezooms|MSNBot|Exabot|MJ12bot|sogou\sspider|YandexBot|bitlybot|ia_archiver|proximic|spbot|ChangeDetection|NaverBot|MetaJobBot|magpie-crawler|Genieo\sWeb\sfilter|Qualidator.com\sBot|Woko|Vagabondo|360Spider|ExB\sLanguage\sCrawler|AddThis.com|aiHitBot|Spinn3r|BingPreview|GrapeshotCrawler|CareerBot|ZumBot|ShopWiki|bixocrawler|uMBot|sistrix|linkdexbot|AhrefsBot|archive.org_bot|SeoCheckBot|TurnitinBot|VoilaBot|SearchmetricsBot|Butterfly|Yahoo!|Plukkie|yacybot|trendictionbot|UASlinkChecker|Blekkobot|Wotbox|YioopBot|meanpathbot|TinEye|LuminateBot|FyberSpider|Infohelfer|linkdex.com|Curious\sGeorge|Fetch-Guess|ichiro|MojeekBot|SBSearch|WebThumbnail|socialbm_bot|SemrushBot|Vedma|alexa\ssite\saudit|SEOkicks-Robot|Browsershots|BLEXBot|woriobot|AMZNKAssocBot|Speedy|oBot|HostTracker|OpenWebSpider|WBSearchBot|FacebookExternalHit [NC,OR]
      RewriteCond %{QUERY_STRING} _escaped_fragment_

      # Only proxy the request to Prerender if it's a request for HTML
      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))(.*) http://service.prerender.io/http://example.com/$2 [P,L]
  </IfModule>

  # If the requested resource doesn't exist, use index.html
  RewriteRule ^ /index.html

1 个答案:

答案 0 :(得分:0)

您有此部分:

  RewriteCond %{REQUEST_URI}  ^/$
  RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
  RewriteRule ^(.*)$ /snapshots/%1? [NC,L]

如果网址中包含_escaped_fragment_,则会尝试从/ snapshots /目录中提供文件。这与Prerender.io没有任何关系,所以你可能想删除该部分,因为它可能是404的原因。

你也在用他们的用户代理检查Googlebot和Bingbot,这是一个坏主意,因为他们可能会因为伪装而惩罚你。