Skip to content

Instantly share code, notes, and snippets.

@dmdeller
Created November 6, 2011 17:01
Show Gist options
  • Save dmdeller/1343176 to your computer and use it in GitHub Desktop.
Save dmdeller/1343176 to your computer and use it in GitHub Desktop.
Tiny proxy to redirect Yahoo searches from the Safari search bar to DuckDuckGo
Options +ExecCGI +FollowSymLinks
RewriteEngine On
RewriteRule ^search duckduckgo.rb
#!/usr/bin/env ruby -w
=begin
For this to work, add the following line to /etc/hosts:
127.0.0.1 search.yahoo.com
Searches using 'Yahoo' in the Safari search bar will now redirect to DuckDuckGo (or somewhere else if you change REPLACEMENT_URL). You can still search Yahoo directly at www.yahoo.com.
=end
require 'cgi'
REPLACEMENT_URL = 'https://duckduckgo.com/?q=%s'
ALT_YAHOO_SERVER = 'search.ystg1.b.yahoo.com'
# I should be able to get this from the web server, but couldn't figure out how... equivalent to PHP's $_SERVER['REQUEST_URI']
REQUEST_PATH = '/search'
cgi = CGI.new
if (cgi['fr'] == 'aaplw') then
# This search came from the Safari search box... redirect to preferred search engine
query = cgi['p']
print cgi.header(
'status' => 'REDIRECT',
'location' => sprintf(REPLACEMENT_URL, CGI::escape(query))
)
else
# Search didn't come from Safari. Redirect back to Yahoo.
print cgi.header(
'status' => 'REDIRECT',
'location' => 'http://' + ALT_YAHOO_SERVER + REQUEST_PATH + '?' + cgi.query_string
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment