How To Easily Create Your Own URL Shortener With WordPress

This morning I set up a site with it’s own URL “shortening service” so to speak and some folks thought you guys might want to know how I did it. With all the talk of Tweet-Jacking and concerns regarding the branding you’re doing for URL shortening services instead of yourself, it only makes sense to create your own internal URL shortening service if at all possible.

Thanks to Michael Gray knowing at the drop of a dime that the max number of characters Twitter will display for a URL is 30, I did a quick count of our full URL:

http://adomainurlhere.com/ = 26 characters

(A lot of you have asked why I often non www with URL structure and now you know why… yes Michael, you taught me something a few years ago even if at the time you were thinking print space and not URL shortening.)

Since it’s entirely possible to shorten our own URLs and I’m not super technical, I went in search of a WordPress plugin that could help me along in the process and came across Link Shortcut. This appeared to have all the functionality I wanted (you can define random strings or generate your own, you can specify a sub-folder if you want and you can set a default length for the URL to ensure you don’t go over the “Twitter max”.)

I installed the plugin, left the option for a sub directory blank (we’re close to the max without one) and set the default character length to 4 in the options (the 26 characters from our root domain plus 4 characters in our shortened URL = 30). Next, I manually setup a shortened URL the same way I would with Tinyurl.com or any other shortening service in my WordPress backend. Worked like a charm.

Until I checked the server header generated by the URL… which was a 302. I immediately asked the plugin author if he could change the redirect from a 302 to a 301 for SEO reasons, but I’m also impatient. Working with Thesis on a regular basis and learning about hooks has made me a little bit braver when it comes to nosing around PHP.

By opening all of the plugin files, I was able to identify that link-shortcut > lib > linkshortcut.php was the file that contained the function that “called out” to WordPress’s default redirection command (I am not a programmer so if my terminology is wrong, so be it):

/**
* Get final destination based on query and redirect user.
*/
function go(){
//determine final destination
$final_destination = $this->getFinalDest();
//redirect user
wp_redirect($final_destination);
}

The “wp_redirect” mentioned there is a default redirect function of WordPress which is still a 302 despite numerous complaints. That thread showed me that I could change the 302 to a 301 by making the following edit:

/**
* Get final destination based on query and redirect user.
*/
function go(){
//determine final destination
$final_destination = $this->getFinalDest();
//redirect user
wp_redirect($final_destination,301);
}

So I edited the file, saved it and uploaded the entire plugin again and it worked*. We now have our own URL shortening service right in our WordPress backend that 301 redirects the shortened URLs, making them as SEO friendly as possible.

*IMPORTANT NOTE: unless the plugin author makes this change to the plugin himself in future releases, you will need to RE-EDIT the above code every time you upgrade it.

EDITED TO ADD: The plugin author did update it so that you don’t have to edit the code yourself to make the redirect a 301.

Sure, we can’t control if others use outside shortening services to shorten our URLs, but we can at least control our own generation of shortened URLs. We can promote URLs that contain our branding and increase the trust of folks who might be wary about clicking through on those links if they were using a URL they didn’t recognize. And we can worry a bit less about URL shortening services shutting down, redirecting or breaking URLs we’ve promoted with our name attached to them.

EDITED TO ADD:

Aaron Chronister of Bacon Explosion and BBQ Addicts fame wrote us a kick ass function to update our tweet this button to use the shortened url (if one exists) while using this plugin. And then he said we could keep it a secret or share it, so here ya go!

/* Grab Custom Short URL */
function grab_short_url() {
mysql_connect(“localhost”,”username”,”password”) or die(mysql_error());
mysql_select_DB(“database_name”);
$result = mysql_query(“SELECT * FROM wp_linkshortcut WHERE
url=’http://outspokenmedia.com” . $_SERVER[“REQUEST_URI”] . “‘”);
if (mysql_num_rows($result)==0) {
$short_url = “http://outspokenmedia.com” . $_SERVER[“REQUEST_URI”] . “”;
echo $short_url;
} else {
$short_url = “http://outspokenmedia.com/” . mysql_result($result,0,ident) . “”;
echo $short_url;
}
mysql_close();
}

Be sure to change the username, password and database name to be your own. Then, instead of calling <?php the_permalink() ?> in your “tweet this” link, change it to <?php grab_short_url() ?> and you’re all set. YOU ROCK AARON! :)

About Rae Hoffman

Rae Hoffman aka "Sugarrae" is an affiliate marketing veteran and the CEO of PushFire, a search marketing agency specializing in SEO audits and link building strategies. She is also the author of the often controversial Sugarrae blog. You can connect with Rae via Twitter, Google+ and Facebook.

Sugarrae runs on the Genesis Framework

Genesis Framework

If you’re someone who doesn’t understand a lot of PHP, Genesis will give a ton of functionality that you wouldn’t be able to obtain otherwise with a simple control panel instead of having to alter code. For the advanced, Genesis has incredible customization possibilities via Genesis hooks.

The theme is not only highly customizable, but it has allowed me to run Sugarrae more professionally, with a much more targeted focus on monetization than it ever has been able to achieve before.

You can find out more about Genesis below:

Comments

  1. Not sure how old this article is, but it’s worth noting that your example has a security vulnerability in it:

    $result = mysql_query(“SELECT * FROM wp_linkshortcut WHERE
    url=’http://outspokenmedia.com” . $_SERVER[“REQUEST_URI”] . “‘”);

    That line is inserting whatever’s in the url directly into a mysql query, which allows a savvy malicious person direct access to your database (called mysql injection). With that, they can run whatever crafty queries they want, including and up to deleting the entire database.

  2. I was going to point out the same vulnerability, but Mr. Rasmus beat me to the punch several months back. :) I just went back and noticed this post was from early 2009.

Speak Your Mind

*