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

Asked

Viewed 136 times

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 you 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 requested "http://www.22find.com/web?q=PESQUISA", is redirected to search.yahoo.com/search?q=PESQUISA.

1 answer

2

To fetch the query string you can use:

location.search.slice(3)

Then all you have to do is concatenate this string into the url you have:

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 find the current address, with querystring and removing the ?q=.
In the second line generates a new string with matching the url you want.
In the third line makes the re-direct.

To use this on your site 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;
};
  • Sorry, I was busy. And as I apply this in the form?

  • <form="get" action="https://br.search.yahoo.com/yhs/search"> <input id="search-box" name="p" type="text" autofocus value="" autocomplete="off"/> <input type="Hidden" id="hspart" name="hspart" value="Oninc"> <input type="Hidden" id="hsimp" name="hsimp" value="yhs-selfserve_5414a9bb52031219"> <input type="image" src="Buttons/Submit.png" value=""> </form>

  • Now a little bit I bumped into this snippet to pull all the vars of the query: http://stackoverflow.com/a/21152762

  • @Gustavobarbosa if you want to do it via client (javascript) then you should not submit the form. You must stop Submit and redirect without going through the server. That’s what you want?

  • I want to have my server’s /search page redirect the search to Yahoo. Ex: Click on the link and see: http://www.22find.com/web?q=suasearch

  • @Gustavobarbosa, test add this code that I have now added to the answer at the bottom of your page. Before the </body>

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.