How to redirect question "q=" from one site to another without changing the question?

2

I have a mini search site: http://www.cosmo-search.url.ph , and when typed

www.cosmo-search.url.ph/search?q=PESQUISA-FEITA-PELO-USUÁRIO 

I need to be redirected to

br.search.yahoo.com/yhs/search?p=PESQUISA-FEITA-PELO-USUÁRIO&hspart=Ballooninc&hsimp=yhs-selfserve_5414a9bb52031219

Following the example of 22find, which when asked for "http://www.22find.com/web?q=PESQUISA" , is redirected to search.yahoo.com/search?q=PESQUISA .

    
asked by anonymous 22.09.2014 / 23:50

1 answer

3

To get the query string you can use:

location.search.slice(3)

Then you just have to concatenate this string in the url that has:

var string = location.search.slice(3);
var novoUrl = ['br.search.yahoo.com/yhs/search?p=', string,'&hspart=Ballooninc&hsimp=yhs-selfserve_5414a9bb52031219'].join('');
window.location = novoUrl;

In the first line you will get the current address, with the querystring and removing the ?q= . In the second line generates a new string with matching the url you want.
On the third line do the re-direct.

To use this on your site you can do so:

document.getElementsByTagName('form')[0].submit = function (e) {
    e.preventDefault();
    var string = document.getElementById('#search-box').value;
    var novoUrl = ['br.search.yahoo.com/yhs/search?p=', string, '&hspart=Ballooninc&hsimp=yhs-selfserve_5414a9bb52031219'].join('');
    window.location = novoUrl;
};
    
22.09.2014 / 23:55