google trends II

I wanted to reply to a question elsewhere on the site, but a ‘comment’ box isn’t fit for it so I’ll put the reply here. The question was about creating ‘search engine friendly’ descriptive URL’s based on keywords from the Google Trends atom feed, listing pages a graph of the trend.

You can get a site to list http://domain.com/trend_title.html type url’s by using mod_rewrite, an apache module.

In the server directory of the application you can use an .htaccess file to set rules for file access in these folders. When the server gets request from browsers or servers it applies any rewriting rules you define in .htaccess to these requests.

I tried this one :

  1. <ifmodule mod_rewrite.c>
  2.  RewriteEngine On
  3.  RewriteCond %{REQUEST_FILENAME} !-f
  4.  RewriteCond %{REQUEST_FILENAME} !-d
  5.         RewriteRule ^(.*).html /trendinfo.php?title=$1
  6. </ifmodule>

RewriteEngine On
sets the rewrite mechanism on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

tell the apache server that rewriteconditions apply to file-requests that are not an existing file (F) or directory (D). If the requested filename is anywhere in the servers file table, the server dishes out that file, otherwise it will try to apply a RewriteRule. Applying the rule generates a new request, if that returns anything, the server dishes that out, otherwise it returns an htpp-404 ‘file not found’.

The actual url rewrite rule is :
RewriteRule ^(.*).html /trendinfo.php?title=$1
which means :

  • if any filename is requested that satisfies the mask ^(.*).html then
  • take everything before .html
  • add that as variable $1 to trendinfo.php?title=$1
  • see if it sticks

If the browser requests http://domain.com/bob+bowersox.html, the server will assert it is not a file or directory on the server, and test the available rules. When it notices it the requested file ends with .html, it applies the rewrite rule and tries to access http://domain.com/trendinfo.php?title=bob+bowersox.

A browsing user does not notice a thing.

In trendinfo.php I wrote some code to handle the ‘new’ request :

  1. if(!isset($_REQUEST['title'])) {
  2. //if there is no $1, added as title, fake a 404 "file not found" message
  3.         echo 'the emptiness…';
  4. } else {
  5. //get the title from the request
  6.   $mytitle=htmlentities($_REQUEST['title'], ENT_QUOTES, "UTF-8");
  7. //put the google trends graph url together
  8.   $graphurl = 'http://www.google.com/trends/viz?hl=&q=';
  9.   $graphurl .= urlencode($mytitle);
  10.   $graphurl .= '&date=';                        //leave date blank to get the current graph
  11.   $graphurl .= '&graph=hot_img&sa=X';
  12.   echo "<img class=hotGraph width=280 height=190 src='$graphurl'/>";
  13. }

…that outputs the Google trend graph on the url http://domain.com/bob+bowersox.html

You can also put this in index.php :

  1.   $feed = simplexml_load_file('http://www.google.com/trends/hottrends/atom/hourly');
  2.   $children =  $feed->children('http://www.w3.org/2005/Atom');
  3.   $parts = $children->entry;
  4.   foreach ($parts as $entry) {
  5.      $details = $entry->children('http://www.w3.org/2005/Atom');
  6.       $dom = new domDocument();
  7.      $html=$details->content;
  8.      @$dom->loadHTML($html);
  9.       $anchors = $dom->getElementsByTagName('a');
  10.     foreach ($anchors as $anchor) {
  11.       $url = $anchor->getAttribute('href');
  12.       $urltext = $anchor->nodeValue;
  13.      echo '<a href="'.urlencode($urltext).'.html" target="_blank">'.$urltext.'</a> ';
  14.     }
  15.    }
  16.    unset($dom);
  17.    unset($anchors);
  18.    unset($parts);
  19.    unset($feed);

That lists the current 100 google trends with a link. If you use the .htaccess rewrite rules, the server reroutes all the links to trendinfo.php with descriptive urls.

I hope that helps.

Posted in google, php, seo and tagged , , , .

4 Comments

  1. Good job pal i had try it and nothing found problem because this script very easy to install in web host for newbie like me :)

    I have some question again about this script.

    I tried to replace you script with this parameters for showing related article result :

    $graphurl = 'http://google.com/trends/hottrends?q=';
    $graphurl .= urlencode($mytitle);
    $graphurl .= '&date=';
    $graphurl .= '&sa=X';
    echo $graphurl;

    but it does not happen any changes can you tell me whats wrong?

    Untitled
    Last ask is how to automaticly change the title in browser (not only showing Untitled title)

    Thanks again for your works to answered my question

  2. Herman,
    using < ? echo $_REQUEST['title']; ?> in the meta tags section of the page puts the page title on the title, the content of the entries I tried to answer in ‘google trends III’

Leave a Reply

Your email address will not be published. Required fields are marked *