Community Software 24

Let me help to build your community! Future oriented!

 
    Connect with Facebook

Short URLs for Twitter & Co.

February 16, 2010 by Torsten Wesolek   Comments (0)

The following is a general issue, and you can easy abstract this for other software, eg wordpress. I will write this in relation of elgg, of course.

If webdevelopers have the the task to work with short urls, e.g. if the want to use twitter to link back to their articles, they often decide to use a url shortening service like bit.ly or other. A short url then looks like http://bit.ly/abzxy .

Also google will offer such a service under the address goog.le.

As you will know, the lenght of a twitter message is restricted to 140 characters. A short url is often the only way to link to an article at your website.

But there are a few drawbacks: If the service would be disabled or would go offline for some reason, or if the service provider would take money for every short url redirection, what would you do? May be, all your links could lead to nowhere some day.

But - one of the main problems is the SEO view. One of the main components of Search Engine Optimization is building of backlinks to your site. If you use such a short url service you do not build valuable backlinks. But why?

If the service (e.g. at bit.ly) receives a url call, they will decode this url and redirect your browser to the real address at your site. This works well with your browser, but not with any search engine. Google & Co. will not follow a redirection with a "302 moved permanently" header.

As a result they do not consider these backlinks for calculation of the so importend backlink count to your site.

What can you do?

Assuming, you have a relative short domain name, you can do a something. (community-software-24.com .... hmmmm.... not really short Undecided )
Assuming furthermore you have a webserver with rewrite possibilities (e.g. apache with mod-rewrite enabled) and assuming you have a software system with numerical ID in the database.

1. configure your server

Your server should be configured in the manner to handle the url without the "www". In my cace I should be able to call http://community-software-24.com/

2. build the URL

Find the place in your software, where the URL to your content is build. In Elgg this is the function getUrl() for an entity. Lets say, your url is build in this manner: http://www.community-software-24.com/pg/file/torwe/read/413/tidypics-version-with-flash-upload-tool , your short URL should look like this http://community-software-24.com/413 . You should write a function like getShortUrl() to do this.

3. modify your twitter service plugin

Elgg specific: You have to modify your twitter service plugin to use your shohrt url together with twitter send.

4. redirect script

Drop a script like this in your document root (eg. save as redirect.php):

require_once($_SERVER["DOCUMENT_ROOT"] . '/engine/start.php');

$guid = (int) $_GET["guid"];

$elggobject = get_entity($guid);

if (is_object($elggobject)) {

      $longurl = $elggobject->getUrl();

      if (!strstr($longurl,'http://')) {

                  $longurl = 'http://' . $_SERVER["HTTP_HOST"] . $longurl;

      }

    header("HTTP/1.0 301 Moved Permanently");

    header("Location: $longurl");

    header("Connection: close");

    exit;

}

exit;

 

5. rewrite entry

Create a rewrite entry in the server conf file or in your .htaccess, which maches with such URLs and will rewrite them to your redirect script. As you will notice, the script above will redirect with a search engine friendly 301 header to your entry.

For apache webserver this look like this:

   RewriteRule ^([0-9]+)$ /redirect.php?guid=$1 [L]

Don't forget to restart your server, if you changed your conf file.

That's it.