Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Processing Fragment Identifier in URL

I've got quite a dilemma here and would like some suggestions from the community. Here's my problem:

I've created a website that has lots of dynamic content, all content is pulled via AJAX and displayed via JS. The way I'm currently allowing direct links to my content is by modifying the fragment identifier on the fly with JS, here's an example:

http://www.mysite.com/home#/123

Where 123 is the ID of the content. When the content is closed it strips the fragment identifier off again.

This works wonderfully, and when people load the page my AJAX request is successfully requesting the '123' article. But I've just implemented a Facebook like button on all the articles, and now I've hit a brick wall.

Basically, the way Facebook finds what thumbnail and titles and URL to associate with your 'liked' content is to check the meta tags of the URL passed into the fb like iframe. It does this by doing a GET request at the URL, checking the content of the meta tags, and then displaying those on Facebook.

The problem is that fragment identifiers are not passed to the server, so I have no way in PHP (that I'm aware of) to dynamically generate these meta tags with the content from the specifically requested article.

For example, I wanted to be able to do something like this:

$id_vals = get_id_values($hash_fragment);
echo '<meta property="og:title" content="'.$id_vals['title'].'" />';

Your first thought is probably: Why not use a GET request or query string? i.e.:

http://www.mysite.com/home?id=123

However the problem with this approach is that Javascript cannot dynamically change this portion of a URL, it can only change the fragment identifier. This means that all direct links to our articles will require page refreshes. This goes against our whole 'dynamic' approach we have going - which I might add is really nice :)

So I'm kind of stumped at the moment. The only semi-solution I can think of is to use an approach of both:

In the fb like requests use get parameters and for direct links use fragment identifier.

But this poses the following problems:

  • If a direct link is pasted on Facebook from a user it will include the fragment identifier, and the correct meta data won't display on Facebook.
  • I would need to do some weird redirection magic to fix the format of the URL to remove the ID (i.e. detect if GET parameter is passed in and redirect to the fragment identifier equivalent). But I don't know if FB would also follow the redirect and fetch the meta tags from the redirected page instead of the first page, which would effectively make this solution useless anyway.

Sorry for the wall of text! Appreciate any input you can provide.

EDIT: I just tested my proposed solution involving a combination of both, and I can confirm Facebook follows the redirect, so this solution doesn't work at all. This is depressing!

EDIT EDIT: One possible way to implement my 'mix' idea is to use a window.location redirect instead of a PHP header() call redirect, so Facebook fetches the meta tags from the original page, for those of you with a similar problem.

Thanks.

like image 260
scrapii Avatar asked Jun 09 '12 03:06

scrapii


People also ask

What is URL fragment ID?

The fragment identifier introduced by a hash mark # is the optional last part of a URL for a document. It is typically used to identify a portion of that document. The generic syntax is specified in RFC 3986. The hash-mark separator in URIs is not part of the fragment identifier.

Is fragment identifier sent to server?

Fragment identifiers are not sent to the server. The hash fragment is used by the browser to link to elements within the same page.

How do you define a fragment identifier?

HTML provides two ways to identify a document fragment: by inserting an anchor ( a ) element with the name attribute (instead of href ) or by adding the id attribute to any HTML element.

How do URL fragments work?

A fragment is an internal page reference, sometimes called a named anchor. It usually appears at the end of a URL and begins with a hash (#) character followed by an identifier. It refers to a section within a web page. In HTML documents, the browser looks for an anchor tag with a name attribute matching the fragment.


1 Answers

If you can rejigger your site to use hashbang #! URLs, Facebook somewhat quietly supports Google's AJAX crawling spec.

On encountering a URL containing #!, it will automatically convert that and the following text to the GET-friendly query string ?_escaped_fragment_=. Your server-side code can key off that GET param, if it's present.

like image 182
John Flatness Avatar answered Oct 10 '22 19:10

John Flatness