Adding a URL hash into meta data for Facebook and

2019-07-27 12:12发布

问题:

I am looking to use a hash that is given in through a URL (www.example.com/index.html#myhash) and use it in the page's metadata that Facebook and Twitter can then retrieve to make a share card like this: https://dev.twitter.com/docs/cards/types/photo-card. Is this possible? Or would it be better to just use a PHP $_GET[] variable? I'm trying to keep the page static so the hash would be ideal, but I'm fairly sure I can't dynamically create the meta tag with JS because Twitter and Facebook will not execute that code. Thanks for help!

回答1:

Correct, creating the meta elements client-side will not work because those crawlers don’t execute any client-side scripting code, they just look at the HTML code they get.

And the hash part of an URL is only of client-side importance, so it does not even get passed to the server when making an HTTP request – so generating those meta elements server-side based on the hash is also not possible.

And finally, changing the hash part of an URL does not make it a different URL – so this is not compatible with Facebook’s Open Graph philosophy, where the equation is one URL == one OG object.