Skip to content

Instantly share code, notes, and snippets.

@duartealexf
Created September 16, 2019 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duartealexf/9ad574052458bf8a6edcec24857e19c7 to your computer and use it in GitHub Desktop.
Save duartealexf/9ad574052458bf8a6edcec24857e19c7 to your computer and use it in GitHub Desktop.
apache virtualhost config for prerender.io
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,QSA,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example
# LogLevel rewrite:trace5
ErrorLog ${APACHE_LOG_DIR}/example-error.log
CustomLog ${APACHE_LOG_DIR}/example-access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
<IfModule mod_dir.c>
DirectoryIndex index.html
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
# Remove www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [R=301,QSA,NC,L]
# Redirect trailing slashes if not a folder
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,QSA,R=301]
<IfModule mod_proxy_http.c>
<IfModule mod_headers.c>
RequestHeader set X-Prerender-Token "XXXXXXXXXXXXXXXXX"
RequestHeader set X-Prerender-Version "prerender-apache@2.0.0"
</IfModule>
SSLProxyEngine on
RewriteCond %{HTTP_USER_AGENT} googlebot|bingbot|yandex|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest\/0\.|pinterestbot|slackbot|vkShare|W3C_Validator|whatsapp [NC]
RewriteCond %{REQUEST_URI} ^(?!.*?(\.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|\.svg))
RewriteRule ^(.*) https://service.prerender.io/%{REQUEST_SCHEME}://%{HTTP_HOST}$1 [P,END]
</IfModule>
# Redirect routes to index
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^ /index.html [L]
</IfModule>
<Directory /var/www/example>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
</IfModule>
@duartealexf
Copy link
Author

No .htaccess is needed.

Note that the escaped_fragment has also been removed. See https://webmasters.googleblog.com/2015/10/deprecating-our-ajax-crawling-scheme.html.

It fixes a "double slashes" issue, which happened when using config from official Prerender.io docs (e.g. it crawled and logged http://example.com//favicon.ico).

@duartealexf
Copy link
Author

This is for Apache 2.4 only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment