Search code examples
cnginxurl-rewritinggooglebot

Serving index.html to the Google bot with ?_escaped_fragment_=


I have a very Javascript heavy application which I'd like to get indexed. I have a directory of snapshots on my website which can be served to the Google bot. These are under mysite.com/snapshots/.

I'm using the following rewrite rule to serve the snapshot to the Google bot:

location / {
    if ($args ~ "_escaped_fragment_=") {
        rewrite ^/(.*)$ /snapshots/$1.html break;
    }
}

This works for all of my snapshots except the home page. The problem is, the homepage gets saved as mysite.com/snapshots/index.html. When the Google bot requests mysite.com/?_escaped_fragment_=, nginx tried to serve /snapshots/.html and obviously the request 404's.

I need to adapt the rewrite rule to serve up index.html when the document root is requested.

Cheers!


Solution

  • Above your rewrite line add:

    rewrite ^/$ /index.html;