30

I understand the og:url meta tag is the canonical url for the resource in the open graph.

What strategies can I use if I wish to support 301 redirecting of the resource, while preserving its place in the open graph? I don't want to lose my likes because i've changed the URLs.

Is the best way to do this to store the original url of the content, and refer to that? Are there any other strategies for dealing with this?

To clarify - I have page:

/page1, with an og:url of http://www.example.com/page1

I now want to move it to /page2, using a 301 redirect to http://www.example.com/page2

Do I have any options to avoid losing the likes and comments other than setting the og:url meta to /page1?

Igy
  • 43,710
  • 8
  • 89
  • 115
timmow
  • 3,595
  • 2
  • 23
  • 22

2 Answers2

49

Short answer, you can't.

Once the object has been created on Facebook's side its URL in Facebook's graph is fixed - the Likes and Comments are associated with that URL and object; you need that URL to be accessible by Facebook's crawler in order to maintain that object in the future. (note that the object becoming inaccessible doesn't necessarily remove it from Facebook, but effectively you'd be starting over)

What I usually recommend here is (with examples http://www.example.com/oldurl and http://www.example.com/newurl):

  • On /newpage, keep the og:url tag pointing to /oldurl
  • Add a HTTP 301 redirect from /oldurl to /newurl
    • Exempt the Facebook crawler from this redirect
    • Continue to serve the meta tags for the page on http://www.example.com/oldurl if the request comes from the Facebook crawler.
    • No need to return any actual content to the crawler, just a simple HTML page with the appropriate tags

Thus:

  1. Existing instances of the object on Facebook will, when clicked, bring users to the correct (new) page via your redirect
  2. The Like button on the (new) page will still produce a like of the correct object (but at the old URL)

If you're moving a lot of URLs around or completely rewriting your URL scheme you should use the new URLs for new articles/products/etc, but you'll need to keep the redirect in place if you want to retain likes, comments, etc on the older content.

This includes if you're changing domain.

The only problem here is maintaining the old URL -> new URL mapping somewhere in your code, but it's not technically difficult, just an additional thing to maintain in the future.

BTW, The Facebook crawler UA is currently facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)

Igy
  • 43,710
  • 8
  • 89
  • 115
  • If the old URL has gone through a 301 redirect, and then follow your recommendations, would the fb like count still reset to 0? – Fadhli Rahim Apr 22 '13 at 15:26
  • If you had likes on a URL, then made that URL redirect elsewhere, will the target of the redirect retain the like count of the old URL? No, I'm fairly sure it does not. that's almost the opposite to the workaround my answer provides – Igy Apr 22 '13 at 18:49
  • Why is it needed to exempt the facebook crawler from the redirect? What happens if you wouldn't do that? – Lode Jun 10 '14 at 13:15
  • I'm not 100% sure, but i believe the crawler considers the 'post-redirect' page to be the canonical URL and may ignore likes, comments, etc at the 'old' URL (which is now in the middle of a redirect chain to the final, canonical URL) – Igy Jun 10 '14 at 15:49
  • Ugh, -10 to Facebook for making this the only way and not offering us guidance. Thanks for the solution but for most people this is still too complicated and would involve a lot of disproportionate complexity. – jerclarke May 18 '15 at 17:24
  • On the bright side likes are much more important for new content than old stuff, so starting fresh isn't the end of the world. – jerclarke May 18 '15 at 17:25
  • 1
    this is what FB currently says about their crawler (including updated UA) - plus it's good to see they authorize detecting UA so it's ok to do that - https://developers.facebook.com/docs/sharing/webmasters/crawler – Simon_Weaver Sep 26 '15 at 00:46
  • Yep, that documentation page is partially based on my answer here - i forgot to add a link here after it was published in April - thanks for providing it – Igy Sep 28 '15 at 18:01
  • What if I have totally change the domain and I am doing this in htaccess , at this point there is no way to open old page , any idea how to do that? – Karim Samir Nov 01 '15 at 12:02
  • What do you mean 'there's no way to open old page'? you need to have the old URL still respond to Facebook's crawler for the answer above to work, if it can't for some reason, you're going to be starting from 0 with the URLs on the new domain / the new URLs – Igy Nov 01 '15 at 19:58
  • as of now, this answer is no longer true. see https://developers.facebook.com/docs/plugins/faqs – törzsmókus Dec 28 '16 at 00:31
  • I believe the FAQ is still consistent with this answer: ( from 'How do I move a page to a different URL?' on that URL) `This also requires that the old URL still renders a document with Open Graph tags and returns a HTTP 200 response, at least when loaded by Facebook's crawler. If you want other clients to redirect when they visit the URL, you must send your 301 HTTP response to all non-Facebook crawler clients. The old URL should contain its own og:url tag that points to itself.` – Igy Jan 03 '17 at 15:33
0

I'm having the same problem with my old sites. Domains are changing, admins want to change urls for seo etc

I came to conclusion its best to have some sort uniqe id in db just for facebook - from the beginning. For articles for example I have myurl.com/a/123 where 123 is ID of the article.

Real url is myurl.com/category/article-title. Article can then be put in different category, renamed etc with extensive logic for 301 redirects behind it. But the basic fb identifier can stay the same for ever.

Of course this is viable only when starting with a fresh site or when implementing fb comments for the first time.

Just an idea if you can plan ahead :) Let me know what you think.

Miha Trtnik
  • 236
  • 1
  • 3
  • 8
  • This doesn't solve a protocol change migration (eg. from HTTP to HTTPS). – Agis Apr 02 '15 at 14:34
  • 5
    This makes sense but just makes me glad I never implemented Facebook comments! What an awkward mess. FB should support 301 correctly and re-attribute likes. They should also support HTTP/HTTPS by default. – jerclarke May 18 '15 at 17:23