# @src https://gist.github.com/thoop/8072354#comment-1358615
<IfModule mod_rewrite.c>
RewriteEngine On
# If requested resource exists as a file or directory
# (REQUEST_FILENAME is only relative in virtualhost context, so not usable)
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
# Only exception is /index.htm, /index.html
RewriteCond %{REQUEST_URI} !/index.html?
# Go to it as is
RewriteRule ^ - [L]
# If non existent
# If path ends with / and is not just a single /, redirect to without the trailing /
RewriteCond %{REQUEST_URI} ^.*/$
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)/$ $1 [R,QSA,L]
# If path that is not empty or / or /index.htm or /index.html, redirect to /#!/path
RewriteCond %{REQUEST_URI} !(/index.html?|/|)$
RewriteRule ^(.*)$ /#!$1 [R,QSA,NE,L]
# If not /, redirect to it.
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^ / [R,QSA,L]
# Handle Prerender.io
RequestHeader set X-Prerender-Token "YOUR_TOKEN"
RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
RewriteCond %{QUERY_STRING} _escaped_fragment_=([^&]*)
# Proxy the request
RewriteRule ^ http://jpp-prerender.herokuapp.com/http://%{HTTP_HOST}/?_escaped_fragment_=%1 [P,L]
</IfModule>