cyborganthropology.com is quickly becoming the default online resource for everything relating to Cyborg Anthropology. With over 800 articles and growing daily, it's possible to find nearly anything you can think of.
We recently registered the domain "anth.ro" to use as a URL shortener to articles on cyborganthropology.com. I suggested creating a business card such as this, which would allow Amber to write in arbitrary text following the URL after having a conversation with someone and giving them the card.
Since she would obviously be offline while handing out cards, there would be no way to know for sure if a certain URL existed. The goal is to be able to write down any topic on the card and have the recipient be taken to the corresponding content when they get back to their computer.
Site search
If you didn't already know, you can add site:cyborganthropology.com
to the end of any search in Google to restrict your search to a specific website. Try it here: google.com/search?q=Second+self+site:cyborganthropology.com. This is a great start, because this lets us search the wiki for anything using Google’s excellent search algorithms and spelling correction.
I'm Feeling Lucky
Since we've restricted the search to this site, we can be reasonably confident that the first match is the one we intended. If we were to press Google's "I'm feeling lucky" button, we'd probably get a good result. A quick Google search revealed that I can craft a URL which will automatically redirect to the first result, just like what happens when you press the button!
Adding btnI=I'm+Feeling+Lucky
to the query string will automatically redirect the visitor.
Now our full search URL looks like this. Clicking this link will redirect you to the first page on the wiki matching "Second self".
www.google.com/search?q=Second+self+site:cyborganthropology.com&btnI=I'm+Feeling+Lucky
Short Domain
Now let's put that together with the short domain, anth.ro. We can easily set up a small script to redirect "http://anth.ro/second+self" to the full google search query. However, we'd ideally like to get some better Google analytics on how many times this URL shortener was used, so we'll need to add some tracking parameters to the URL. Since we lose control of the visitor if we send them straight to Google, instead, we can query Google ourselves first to get the destination URL.
Here is a short PHP script which will do the following:
- Query Google with the "I'm feeling lucky" option to find the best match on our site
- Add our additional tracking parameters
- Redirect the browser to the page on the wiki with the tracking code
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/search?btnI=I\'m+Feeling+Lucky&q=' . urlencode($_GET['code']) . '+site%3Acyborganthropology.com'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); // Don't follow redirects, we want the first Location header curl_setopt($ch, CURLOPT_HEADER, TRUE); // Return the headers in the output curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla"); // Set the user agent to Mozilla so Google doesn't block the query curl_setopt($ch, CURLOPT_NOBODY, TRUE); // Don't include the response body in the output curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Return the response in a variable instead of printing it out $response = curl_exec($ch); // If there was a Location header found, use it, otherwise, Google couldn't find a match if(preg_match('/Location: (.+)/', $response, $match)) { $url = parse_url(trim($match[1])); if($url && is_array($url)) { $query = array(); // If the search result included query string parameters, parse those out if(array_key_exists('query', $url)) { parse_str($url['query'], $query); } // Add the search term as the "q" parameter. This is so Google Analytics will treat this as a "site search" query // and include it in the search analytics. You'll also need to add "q" to the "include site search" section in Google Analytics $query['q'] = $_GET['code']; // Add "anth.ro" as the utm_source $query['utm_source'] = 'anth.ro'; // Add "ShortURL" as the medium. These end up appearing in the "Traffic Sources" list as "anth.ro / ShortURL" $query['utm_medium'] = 'ShortURL'; $q = http_build_query($query); // Redirect the user to the page specified by google with our additional parameters included $location = 'http://cyborganthropology.com' . $url['path'] . '?' . $q; header('HTTP/1.1 302 Moved Temporarily'); header('Location: ' . $location); die(); } } // If there were no results returned from Google's I'm feeling lucky button, redirect to a Google search page // This most likely happens on a typo header('HTTP/1.1 302 Moved Temporarily'); header('Location: http://www.google.com/search?q=' . urlencode($_GET['code']) . '+site%3Acyborganthropology.com');
Search
With this in place, you can now use the anth.ro domain as a search interface to the wiki! You can type in anything after anth.ro and you'll either be taken to a page on the wiki or to a page of search results! We didn’t have to write any search code, we just let Google take care of that since that's what they do best!