Search code examples
javascriptgoogle-chromegoogle-apps-scriptgoogle-searchgoogle-custom-search

Google Custom Search is adding %20 in search URL and Query


I have created a google custom search for my website as below but when i search 2-3 keywords it replaces the spaces with %20 and shows no results:

Result Page and Query

Search Box Code:

<script>
            (function() {
                var cx = '44444:orudazwgyxa';
                var gcse = document.createElement('script');
                gcse.type = 'text/javascript';
                gcse.async = true;
                gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
                var s = document.getElementsByTagName('script')[0];
                s.parentNode.insertBefore(gcse, s);
            })();
        </script>
        <gcse:searchbox-only></gcse:searchbox-only>

Search result code:

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

I don't want %20 in my query instead of spaces but i want to show the keywords as it is. And it should show the result for the searched keywords.

Thanks


Solution

  • It seems the solution to the bad results is taking the "www" out of our target URL for the results page.

    Sounds goofy to me, but hey, it worked.

    So, if anyone else is having the same problem, check your result page URL target and remove the www from it

    So instead of: http://www.yourwebsite.com/search_results.html

    use: http://yourwebsite.com/search_results.html

    And, then it will work!