【问题标题】:Google Custom Search with mod_rewrite search term带有 mod_rewrite 搜索词的 Google 自定义搜索
【发布时间】:2014-02-22 17:44:58
【问题描述】:

我在我的网站上使用具有两页配置的 Google 自定义搜索:当表单从第 1 页发送到第 2 页时,第 2 页显示 SERP。这是我的第 2 页:

<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='this is what I want to search' 之类的东西。

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

有什么帮助吗?

【问题讨论】:

    标签: javascript google-custom-search


    【解决方案1】:

    我认为你不需要那个。为什么要搜索引擎友好的搜索结果 URL?据我了解 SEO,您不希望您的搜索结果在 Google、Bing 等上显示为结果

    在我看来www.example.com/search?q=testwww.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

    【讨论】:

    • 抱歉耽搁了。您的解决方案完美运行,我很高兴您自动获得了赏金!再次感谢!
    猜你喜欢
    • 2013-04-03
    • 2011-01-11
    • 1970-01-01
    • 2019-07-17
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多