Nginx/Apache redirect based on User Agent

I have built a web application with AngularJs2, when I tried to share one of my page with my Facebook friends, I’d paste the link into the status update box and hope to see my page scrapped and information from OpenGraph protocol used, but unfortunately I saw:

The reason it looks so bad is that crawlers that scrape HTML don’t interpret JavaScript code.

The solution basically is to use some kind of server-side user detection and redirect crawlers to html generated content:

Configuring Nginx to redirect Facebook/Google/Twitter and Pinterest crawlers to html generated page:

Nginx ( add for your current domain configuration ):

if ($http_user_agent ~* "^facebookexternalhit/[0-9]|Twitterbot|Pinterest|Google.*snippet$") {
   rewrite ^/page/article/(.*)$$1 last;


Apache (.htaccess file):

<ifModule mod_rewrite.c>
RewriteEngine On

# allow social media crawlers to work by redirecting them to a server-rendered static version on the page
RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit/[0-9]|Twitterbot|Pinterest|Google.*snippet)
RewriteRule /page/article/(\d*)$$1 [P]


Those lines will redirect all crawlers to HTML generated content that contains headers with OpenGraph content.


For debugging I used cURL:

curl -A "facebookexternalhit/1.1" -v

* Connected to ( port 80 (#0)
> GET /page/article/1 HTTP/1.1
> Host:
> User-Agent: facebookexternalhit/1.1
> Accept: */*
< HTTP/1.1 302 Moved Temporarily
< Server: nginx/1.10.0 (Ubuntu)
< Date: Wed, 05 Jul 2017 19:04:14 GMT
< Content-Type: text/html
< Content-Length: 170
< Connection: keep-alive
< Location:

Observe header content for “User-Agent” and response header “Location”.

Hope this post will help enable rich social sharing for your one page AngularJS app.

Lasă un răspuns

Adresa ta de email nu va fi publicată. Câmpurile obligatorii sunt marcate cu *

Acest site folosește Akismet pentru a reduce spamul. Află cum sunt procesate datele comentariilor tale.