Skip to content

Instantly share code, notes, and snippets.

@cmandersen
Created July 2, 2014 04:26
Show Gist options
  • Save cmandersen/91d840db222c3df543c2 to your computer and use it in GitHub Desktop.
Save cmandersen/91d840db222c3df543c2 to your computer and use it in GitHub Desktop.
Setup nginx to redirect Google's _escaped_fragment_= request to a snapshots folder
# This should be inserted into the server {...} block.
# These lines rewrite the url to access the snapshots folder, or where ever you keep
# you prerendered pages (mainly used when working with pages rendered by JavaScript).
#
# example.com/?_escaped_fragment_= --> example.com/snapshots/index.html
# example.com/?_escaped_fragment_=/blog --> example.com/snapshots/blog.html
# example.com/?_escaped_fragment_=/blog/post --> example.com/snapshots/blog/post.html
# Captures URLs with any amount of levels (?_escaped_fragment_=/.../...)
if ($args ~ "_escaped_fragment_=(.+)") {
set $real_url $1;
}
# Captures URL with no level (?_escaped_fragment_=)
if ($args = "_escaped_fragment_=") {
set $real_url index;
}
# Redirects the request if the URL contains "_escaped_fragment_="
if ($args ~ "_escaped_fragment_=") {
# You can replace the "snapshots" with whatever folder you keep your prerendered pages in.
rewrite ^ /snapshots/$real_url.html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment