I have a unique setup where my website is built with AngularJS and Wordpress as a single page application. Depending on the routing of the page, I dynamically define meta tags in the controller. Here's a snippet of my HTML header:
<meta property="og:url" content="{{ngMeta['og:url']}}" />
<meta property="og:title" content="{{ngMeta['og:title']}}" />
<meta property="og:description" content="{{ngMeta['og:description']}}" />
<meta property="og:image" content="{{ngMeta['og:image']}}" />
While this setup works correctly, I encountered an issue when trying to share a page on Facebook. The crawler dispatched by Facebook doesn't seem to recognize the meta tags, resulting in incorrect display:
https://i.sstatic.net/VH8Mk.png
I suspect this is due to the fact that the crawler doesn't load the Javascript, hence failing to fetch the meta tags.
To address this, I'm considering implementing a server-side solution using Apache mod rewrite. This involves setting up different redirects for human visitors and social media crawlers (Facebook, Twitter, etc.).
In an article I came across, they suggest using mod rewrite with Apache to handle requests from social media crawlers and redirect them to server-side static pages with metadata processed by the server.
Currently, my website uses Wordpress with permalinks structured as:
https://<dns>/%category%/%postname%/
. Here's a snippet of my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
To implement the suggested solution, I would need to add specific Rewrite rules for each category like so:
RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit/[0-9]|Twitterbot|Pinterest|Google.*snippet)
RewriteRule tracks/(\d*)$ https://<dns>/server/static-page.php?id=$1 [P]
RewriteRule articles/(\d*)$ https://<dns>/server/static-page.php?id=$1 [P]
These rules are intended to redirect crawlers to static pages. However, I'm uncertain how to handle the requests on these server-side static pages. Any suggestions or recommendations?