How to redirect page in PHP to support FB link preview?
I’ve created my own url shortener and I want to achieve the same result as in http://bit.ly case: when you’re trying to share the bit.ly link in FB, you see the nice link preview, but in my case I see only an empty link preview with title and description both set to my url. Here is the code that I use for redirecting:
header( $_SERVER["SERVER_PROTOCOL"]." 301 Moved Permanently" );
header( "Location: ".$url );
Facebook will follow a 301 Redirect to its destination.
Luckily, you can detect the Facebook preview parser’s user agent (
facebookexternalhit) and present it with a page with OG tags instead of redirecting.