# Rewriting pages to re-rendered snapshots

**URL:** <https://www.ruby-forum.com/t/rewriting-pages-to-re-rendered-snapshots/235573>\
**Category:** NGINX\
**Created:** [July 1, 2013, 12:27pm UTC](https://www.ruby-forum.com/t/rewriting-pages-to-re-rendered-snapshots/235573 "2013-07-01T12:27:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@alcina](https://www.ruby-forum.com/u/alcina)\
**Post date:** [July 1, 2013, 12:27pm UTC](https://www.ruby-forum.com/t/rewriting-pages-to-re-rendered-snapshots/235573/1 "2013-07-01T12:27:39Z")

</div>

I have a very Javascript heavy website which is being served by nginx.  
It  
all works great, except Google is having trouble indexing me. I’ve made  
pre-rendered snapshots of all my pages using Phantomjs and these are  
stored  
in /snapshots (relative to my website’s root).

I’ve been using this rewrite rule to detect the Google bot and serve it  
a  
snapshot:

```
    location / {

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

    }

```

It works for all pages apart from the homepage. In the snapshots  
directory,  
the home page is called index.html, however the Google bot requests  
[Website Hosting - Mysite.com](http://mysite.com/?_escaped_fragment_=) when it wants the home page,  
which  
makes this rewrite rule return a 404.

How can I adapt this rule to return the index.html snapshot when / is  
requested?

Thanks!

Posted at Nginx Forum:

> **[Rewriting pages to re-rendered snapshots](https://forum.nginx.org/read.php?2,240435,240435#msg-240435)**
>
> I have a very Javascript heavy website which is being served by nginx. It all works great, except Google is having trouble indexing me. I've made pre-rendered snapshots of all my pages using Phantomjs and these are stored in /snapshots (relative to...

---

<div class="post-metadata">

**Author:** ![rootberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rootberry](https://www.ruby-forum.com/u/rootberry)\
**Post date:** [July 1, 2013, 12:46pm UTC](https://www.ruby-forum.com/t/rewriting-pages-to-re-rendered-snapshots/235573/2 "2013-07-01T12:46:20Z")

</div>

I’m sure others will give you better methods to sort this but, as a  
temporary fix, how about a separate stanza like this:

location = / {  
#special index.html UA-sniffing + rewrite logic  
}

Cheers,  
Jonathan

---

<div class="post-metadata">

**Author:** ![rootberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rootberry](https://www.ruby-forum.com/u/rootberry)\
**Post date:** [July 1, 2013, 12:56pm UTC](https://www.ruby-forum.com/t/rewriting-pages-to-re-rendered-snapshots/235573/3 "2013-07-01T12:56:16Z")

</div>

Not sure what you mean Jonathan, I can already detect the Google bot by  
using ?\_escaped\_fragment= which is also Google’s preferred way over user  
agent sniffing. I just need to adapt the rewrite rule to serve  
index.html to  
requests for /.

Thanks for the reply, though!

Posted at Nginx Forum:

> **[Re: Rewriting pages to re-rendered snapshots](https://forum.nginx.org/read.php?2,240435,240437#msg-240437)**
>
> I have a very Javascript heavy website which is being served by nginx. It all works great, except Google is having trouble indexing me. I've made pre-rendered snapshots of all my pages using Phantomjs and these are stored in /snapshots (relative to...

---

<div class="post-metadata">

**Author:** ![rootberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rootberry](https://www.ruby-forum.com/u/rootberry)\
**Post date:** [July 1, 2013, 1:43pm UTC](https://www.ruby-forum.com/t/rewriting-pages-to-re-rendered-snapshots/235573/4 "2013-07-01T13:43:01Z")

</div>

Duplicate your entire “location / {}” as “location = / {}” (which  
matches _only_ “/”), and when you detect google, serve  
/snapshots/whatever/index.html.

Jonathan
