如何处理?_escaped_fragment_ =用于AJAX抓取工具?

van*_*ert 6 php ajax seo

我正在努力使基于AJAX的网站对SEO友好.正如在网络上的教程中所建议的那样,我href为链接添加了"漂亮" 属性:<a href="#!site=contact" data-id="contact" class="navlink">???????</a>并且,在默认情况下内容加载了AJAX的div中,爬虫的PHP脚本:

$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");
    }
}
Run Code Online (Sandbox Code Playgroud)

我有一种感觉,一开始我需要另外切割_escaped_fragment_=部分,(...)/index.php?_escaped_fragment_=site=about因为否则脚本将无法从URL中GET获取site值,我是对的吗?

但是,无论如何,我如何知道爬虫将漂亮的链接(那些#!)转换为丑陋的链接(包含?_escaped_fragment_=)?我被告知它会自动发生,我不需要提供这种映射,但是Googlebot的抓取并没有向我提供有关URL发生的任何信息.

小智 14

Google bot会自动查询?_escaped_fragment_=网址.

所以从www.example.com/index.php#!site=about 谷歌bot将查询:www.example.com/index.php?_escaped_fragment_=site=about

在PHP站点上,您将获得它 $_GET['_escaped_fragment_'] = "site=about"

如果你想获得"网站"的价值,你需要做这样的事情:

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

看看文档:

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