Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle ?_escaped_fragment_= for AJAX crawlers?

Tags:

ajax

php

seo

I'm struggling to make AJAX-based website SEO-friendly. As recommended in tutorials on the web, I've added "pretty" href attributes to links: <a href="#!site=contact" data-id="contact" class="navlink">контакт</a> and, in a div where content is loaded with AJAX by default, a PHP script for crawlers:

$files = glob('./pages/*.php'); 

foreach ($files as &$file) {
    $file = substr($file, 8, -4); 

}

if (isset($_GET['site'])) {
    if (in_array($_GET['site'], $files)) {
        include ("./pages/".$_GET['site'].".php");
    }
}

I have a feeling that at the beginning I need to additionaly cut the _escaped_fragment_= part from (...)/index.php?_escaped_fragment_=site=about because otherwise the script won't be able to GET the site value from URL , am I right?

but, anyway, how do I know that the crawler transforms pretty links (those with #!) to ugly links (containing ?_escaped_fragment_=)? I've been told that it happens automatically and I don't need to provide this mapping, but Fetch as Googlebot doesn't provide me with any information about what happens to URL.

like image 766
van_folmert Avatar asked Sep 26 '13 13:09

van_folmert


1 Answers

Google bot will automatically query for ?_escaped_fragment_= urls.

So from www.example.com/index.php#!site=about Google bot will query: www.example.com/index.php?_escaped_fragment_=site=about

On PHP site you will get it as $_GET['_escaped_fragment_'] = "site=about"

If you want to get the value of the "site" you need to do something like this:

if(isset($_GET['_escaped_fragment_'])){
    $escaped = explode("=", $_GET['_escaped_fragment_']);
    if(isset($escaped[1]) && in_array($escaped[1], $files)){
          include ("./pages/".$escaped[1].".php");
    }
 }

Take a look at the documentation:

https://developers.google.com/webmasters/ajax-crawling/docs/specification

like image 133
Paulina Avatar answered Sep 18 '22 23:09

Paulina