Articles

Categories

Create Short URL for Twitter

I am not a big Twitter user. However, from time to time I do tweet. On those occasions I am usually passing on information about a blog post or article I found interesting. Which means I need a short URL. While there are a many services such as tinyURL - a quick at the resulting code showed I could do as well or better by redirecting from my own domain.

My domain is www.timr.ca - not as short as is.gd but shorter than tinyurl.com. For the first few tweets I did a quick update of my .htaccess file - then I thought - there must be a better way.

In the next two articles I will look at the 'better way'. The first article (this one) will deal with using a Modx snippet to look after the redirects. The second article will look at how to automated the proceedure. While the articles are written specifically for Modx, the idea can be applied to any management sytem.

Getting a Short Domain Name

Most of you will not be lucky enough to have a short domain name. Don't worry - there are millions available. From short .us and .ca names to ones from countries that don't have residency requirements. Many of these names can be registered for as little as $10.00US a year. Most hosting companies will register the site and host it as part of your existing hosting package.

Once you have registered the domain name you will need to redirect the site to your Modx site - where the real work will take place. To do that simply create a .htaccess file: (Of course if you already have a short domain name you can skip this step.)

RewriteEngine On
RewriteRule ^(.*)$ http://my.site.name/$1 [R=301,L]

The Real Work

The .htaccess File

The real work is done in a single line added to your main .htaccess file and a short snippet.

The following line needs to be added to the top of the .htaccess (after the RewriteEngine On):

RewriteRule ^_(.*)$
http://my.site.name/index-ajax.php?q=assets/snippets/redirect/redirect.php&redirect=$1
[L,R=301]

Note The three lines (above) appear on one line of the .htaccess file with a space between each section. Change my.site.name to your URL.

This code redirects any request for a file beginning with an underscore to the redirect code via the index-ajax.php file. If you already have files starting with an underscore - change the underscore to another character that does not appear as the first character in the filename. You will also need to change the PREFIX in the code to match that character. Note - underscores may appear in other positions in the filename without invoking the redirect.

The Code

Create a directory called 'redirect' in 'assets/snippets/'. Copy and paste the code into a file called redirect.php and move the file to 'assets/snippets/redirect/'.

The code loads first tests to see if there the redirect information has been set. If not it redirects the call to the address set in REDIRECT_TO. It then loads the redirect information from the control Chunk - redirect_list. Stepping through the information one line at a time - it looks for a match. If no match is found it again sends the call to REDIRECT_TO. If a match is found it redirects the call to the new location. Note: - the code does not check to see that the new location is a valid web address.

<?php
// Config
define("REDIRECT_TO","http://www.default.redirect"); // Edit default redirect location
define("PREFIX","_");           // Must match prefix in .htaccess
define("BASE","tw");           // Two letter start of redirect id (optional)

if(isset($_REQUEST['redirect']) && substr($_REQUEST['redirect'],0,2) == BASE) {
     // Get access to $modx
     include_once MODX_BASE_PATH . 'manager/includes/document.parser.class.inc.php';
     $modx = new DocumentParser();
     $modx->getSettings();

     $rawList = $modx->getChunk('redirect_list');
     $listArray = explode(chr(13),$rawList);
     foreach($listArray as $line) {
          $test = explode('::', $line);
          if(PREFIX . trim($_REQUEST['redirect']) == trim($test[0])) {
               header("Location: " . $test[1] ,TRUE,301);
               exit;
          }
     }
}
$location = "Location: " . REDIRECT_TO;
header($location, TRUE, 301);
exit;
?>

Configuration is straight forward. I use 'tw' as the first two letters in my redirects. This is optional. Change

if(isset($_REQUEST['redirect']) && substr($_REQUEST['redirect'],0,2) == BASE) {
to
if(isset($_REQUEST['redirect'])) {

to remove this option and get an extra two characters for your tweet.

The Chunk

Create a Chunk called 'redirect_list'. Enter the redirect information using the following pattern.

_tw[redirect code]::[redirect destination]

I use a simple pattern for the redirect codes. (See below)

_twa::http://blogs.itworldcanada.com/idol/2009/04/24/a-matter-of-survival
_twb::http://blogs.itworldcanada.com/
.
.
.
_twZ::http://http://sethgodin.typepad.com/
_twaa::http://www.dullest.com/blog/
.
.
.
_twZZ::http://99designs.com/

Next Time

In the next article I will automated the proceedure using a popup window.

Leave a Comment





GarykPatton on Tue June 16, 2009, at 06:49:52 wrote


Hi. I like the way you write. Will you post some more articles?