2014-02-22 124 views
1

我在我的网站上使用Google Custom Search进行两页配置:当表单从页面1发送到页面2时,页面2显示SERP。这是我第2页:Google Custom Search with mod_rewrite search term

<script> 
    (function() { 
    var cx = '00000000000'; 
    var gcse = document.createElement('script'); 
    gcse.type = 'text/javascript'; 
    gcse.async = true; 
    gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + 
     '//www.google.com/cse/cse.js?cx=' + cx; 
    var s = document.getElementsByTagName('script')[0]; 
    s.parentNode.insertBefore(gcse, s); 
    })(); 
</script> 
<gcse:searchresults-only queryParameterName="query"></gcse:searchresults-only> 
</script> 

一切工作只要罚款,如第二页是“经典” /page2.php?query=searchTerm。

现在我想使用mod_rewrite切换到友好/ page2/searchTerm。规则本身已完成,但我无法理解如何编辑Google脚本以使其明白现在参数是mod_rewritten或如何使用像gcse.searchTerm ='这就是我想要搜索的内容'。

API v1已被弃用,所以我需要坚持使用API​​ v2。

任何帮助?

回答

3

我不认为你需要那样做。你为什么要搜索引擎友好的搜索结果URL?据我了解SEO,你不想让你的搜索结果中出现为谷歌,必应等

,在我看来www.example.com/search?q=test导致www.example.com/page2/test更“友好”,因为它是比较明显的什么它是。 (搜索结果页。)

但是,也许我正在俯视一些东西,好的。

可能有更好的方法,但我认为这也应该工作:

<script> 
var searchFromURL = function() { 
    var element = google.search.cse.element.getElement('searchfromurlgname'); 
    // you can echo query with PHP or get it from window.location 
    element.execute(window.location.pathname.replace('/page2/', '')); 
}; 
var myCallback = function() { 
    if (document.readyState == 'complete') { 
    searchFromURL(); 
    } else { 
    google.setOnLoadCallback(searchFromURL, true); 
    } 
}; 
window.__gcse = { 
    callback: myCallback 
}; 
(function() { 
    var cx = '013315504628135767172:d6shbtxu-uo'; 
    // Insert your own Custom Search engine ID here 
    var gcse = document.createElement('script'); gcse.type = 'text/javascript'; 
    gcse.async = true; 
    gcse.src = (document.location.protocol == 'https' ? 'https:' : 'http:') + 
     '//www.google.com/cse/cse.js?cx=' + cx; 
    var s = document.getElementsByTagName('script')[0]; 
    s.parentNode.insertBefore(gcse, s); 
})(); 
</script> 

<gcse:searchbox-only></gcse:searchbox-only> 
<gcse:searchresults-only gname="searchfromurlgname"></gcse:searchresults-only> 
<!-- switch CSE layout to "two page" --> 

https://developers.google.com/custom-search/docs/element#tagparams

+0

抱歉耽搁。你的解决方案完美运行,我很高兴你能自动获得赏金!再次感谢! –