How To Easily Create Your Own URL Shortener With WordPress

by Rae Hoffman on 04/21/2009 · 70 comments | Branding

Redirection Crew

This morning I set Outspoken Media up with it’s own URL “shortening service” so to speak and Lisa 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://outspokenmedia.com/ = 26 characters

(A lot of you have asked why we went non www with our 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 the Author

Rae Hoffman

Rae Hoffman is the CEO and Co-Founder of Outspoken Media as well as the author of the often controversial Sugarrae Blog.

Get social with Rae at Sphinn | Twitter | WebmasterWorld

Share this Post

Stay Connected

Subscribe to the Outspoken Media feed via RSS or email and follow Outspoken Media on Twitter!

Related Posts

{ 70 comments… read them below or add one }

1 Brian Clark 04/21/2009 at 12:13 PM

Damn you’re a smart one.

Reply

2 tamaras 01/16/2010 at 11:42 AM

One quiestion: Is it good also for SEO?

Reply

3 Michael Mann 04/21/2009 at 12:14 PM

The 302 is probably used as default as that shows a temporary redirect. However, the 301 provides a permanent redirect, effectively telling search engines not to index the shorter URL. Very good article regarding short URLs.

Reply

4 BarbaraKb 04/21/2009 at 12:16 PM

Whoa… game change, Rae. Much thanks.

Reply

5 Rhea Drysdale 04/21/2009 at 12:20 PM

My brain hurts. Thanks for the lesson Rae!

Reply

6 Seshu 04/21/2009 at 12:24 PM

B.R.I.L.L.I.A.N.T.

Thank you so much for this. I was just asking a wed dev friend about how to do this. He wondered why I would want something like this. For all of the reasons above and then some.

Reply

7 Alysson 04/21/2009 at 12:27 PM

NICE! Something else for my to-do list. Thanks a lot. ;)

Reply

8 Tim Staines 04/21/2009 at 12:28 PM

Wow, it’s really that easy. Saw a couple other posts about custom shorteners, but this one makes it too easy not to implement. Now, how do you add it to the front end of the site so other people can spread your brand for you? Or did I miss that link in the post?

Reply

9 Patrick Sexton 04/21/2009 at 12:28 PM

A useful, actionable article, thanks.

Reply

10 Marty Martin 04/21/2009 at 12:29 PM

Nice job Rae. It’d be awesome to get a plug-in that had a lot of the tracking features, etc. too that other short url services offer (ie: cli.gs, kl.am, etc.)

Reply

11 Rae Hoffman 04/21/2009 at 12:33 PM

Marty – if I could find a RELIABLE PHP developer that actually had the time to make me one, I’d gladly pay for the development :)

Reply

12 TheMadHat 04/21/2009 at 12:38 PM

Don’t forget to modify your “tweet this” button to use your branded domain shortener as well. Should only involve a slight php mod to do it. :)

Reply

13 Rae Hoffman 04/21/2009 at 12:46 PM

Tim, I wouldn’t want just anyone using an outspoken shortened URL… they could shorten a porn URL and then promote it under our brand… that’s one of the ways using your own URL service actually protects your brand is that only you can specify what it redirects to and therefore people who trust your brand can trust those links.

Reply

14 Marko Saric 04/21/2009 at 1:20 PM

Your “tweet this” button still shows the full URL so it is impossible to send the tweet which is inserted automatically without shortening it.

I did something similar on my blog recently. I use my own URL shortener, but it is just the default WordPress page number URL instead of using a plugin. Like “http://yourdomainname.com/?p=2739″. It ends up over 30 characters for my domain but it still goes through without problems. I haven’t heard of the 30 characters URL limit before.

Reply

15 Rae Hoffman 04/21/2009 at 1:26 PM

Marko – Over 30 goes through, but it is cut off and has a … at the end – problem being that if someone copies your tweet and attempts to retweet, they don’t have the full URL in it. Additionally, if the “http://yourdomainname.com/?p=2739″ doesn’t redirect and instead simply displays, you run the risk of duplicate content as far as the engines are concerned and split link popularity (unless you’re using a permalink redirect type plugin). For us, ?p=ID would be too long – we only have four characters to work with.

As for the tweet this button, I’ll be getting with themadhat later to see if he can help us situate that – however, we usually are the first to tweet our links so we at least have a very good shot at getting the branded URL exposure regardless of the tweet this button. As I mentioned, I’d love to find a solid PHP guy to help me develop a plugin that does it all. :)

Reply

16 pageoneresults 04/21/2009 at 1:40 PM

I am so happy to see you ladies bring this to light. Remember to give Jake some love in the process too. Maybe you could hire him to assist with your development?

Anyone ever think about what would happen if one of those URI Shortening Services were hacked? Oh boy, that would really create havoc for the Internet. Imagine a miscreant now having control over millions of shortened URIs that they can redirect at will? I hope I just didn’t give anyone any ideas. :(

Reply

17 A.J. Wood 04/21/2009 at 1:40 PM

Excellent article. I will be implementing the techniques you described ASAP. Thanks for sharing the information.

Reply

18 Rae Hoffman 04/21/2009 at 1:45 PM

pageoneresults I’d love to give youfoundJake some love if I knew 1. what article you were referencing and 2. what it said :)

(the links you posted on Sphinn go to a 404)

Reply

19 pageoneresults 04/21/2009 at 1:51 PM

Rae? I fixed that link right after you replied at Sphinn. I’m tellin’ ya, we need to do something with this short attention span thingy. :)

Create Your Own Short URL and Keep the Branding to Yourself
http://sphinn.com/story/108405

Reply

20 Rae Hoffman 04/21/2009 at 1:57 PM

That’s a link to Sphinn. And when I click on the link at Sphinn to get to the original article, it 404s. Have you tried clicking through to the article? :)

Reply

21 Josh 04/21/2009 at 3:11 PM

Great post. In regards to tracking, wouldn’t you get all the tracking info you need by simply appending your tracking ID (such as you use with Google Analytics) to the URL before you shorten it?

Reply

22 fijidaddy 04/21/2009 at 3:46 PM

cool beans!

Reply

23 Tim Staines 04/21/2009 at 3:47 PM

Fair point, although I’m disappointed in your anti-porn stance :) – - Brand protection and brand exposure don’t always work hand in hand. But doesn’t the fact that the link was dropped by one of you three take care of most of the brand protection piece? In other words, outspokenmedia.com/link from Rae, Lisa, Rhea = good, trusted and brand protected . . . outspokenmedia.com/link from anyone else = just brand exposure & follow at your own risk.
I can see the value both ways . . .
Also, slick re-answer there, I happened to have followed one of your links and then came back to find the Tim/Marty version and just now found answer v.2 :P

Reply

24 Rae Hoffman 04/21/2009 at 3:56 PM

Sorry Tim – wasn’t trying to “re-answer” – I changed it within ten minutes of posting it and figured no one had seen it yet and it made more sense as two separate comments. Sorry for any confusion :)

Reply

25 kathleen 04/21/2009 at 4:06 PM

Genius, just one questions. Any way you can track the number of page views via this?

Reply

26 youfoundjake 04/21/2009 at 4:20 PM

Doh.
Working on fixing the broken link, fluke was not a good first impression. :(
Yay Thesis and 301 integration.
Nicely done again by the women of Outspoken Media!

Reply

27 youfoundjake 04/21/2009 at 4:47 PM

The link has been corrected, so http://sphinn.com/story/108405 should forward you on correctly. oops..heeh
So, the shorturl for this page, where is it located if I wanted to post the short url somewhere? For mine, it automatically posts right after the Tags section on the bottom of the post.

Reply

28 pageoneresults 04/21/2009 at 4:54 PM

Rae, I thought it was the Sphinn link that was borken, sorry. Jake you lose man points or whatever the ladies call them. You better stay on top of that. ;)

“Any way you can track the number of page views via this?”

Yes. It will all be relevant to the tools you have available. I would think the people catching wind of this now are frantically working on the Plugin that provides the tracking. Or, who knows, maybe the creators of these existing Plugins will get the message.

Tracking. Tracking. Tracking. How are you doing it?

Reply

29 redwall_hp 04/21/2009 at 7:21 PM

Or you could use an .htaccess rule to redirect example.org/1234 to example.org/?p=1234, which would, I believe, change to the preferred permalink.

Reply

30 Trace 04/21/2009 at 7:55 PM

Another easy way to do this is using the redirection plugin, which is probably one of my top 3 WP plugins of all time… a MUST HAVE for any WP install!

Simply add the source url (domain.com/shortened) that you want to redirect (it will be 301 unless you choose 302 or 307) and it’s destination (newdomain.com) …. and you’re done!

Reply

31 Tyler Shick 04/21/2009 at 8:09 PM

We recently released a similar WordPress plugin without the run-around and risk of outsiders using your URL. It’s called Synected. We’d love for you to check it out and let us know what you think:)

http://www.blurbia.com/plugins/synected/

Reply

32 John Thornton 11/21/2009 at 10:57 AM

I really like Synected. I installed and it works great (sample size = 1). I tried Link Shortcut but I had an error that was beyond my ability to troubleshoot and I’ve not heard back from my inquiry to the creator of Link Shortcut.

I look forward to future releases of Synected.

Reply

33 Alan Bleiweiss 04/21/2009 at 8:42 PM

“and I’m not super technical…”

LOL Rae, you’re awesome!

Well, I guess this is not an option for me at http://search-marketing-answers.com so I either need to get a new domain and set up 301s just so I can do this, just deal with my loss, or start a whole new business venture built on affiliate marketing as a revenue model like I planned and with THAT, come up with a short URL so I CAN take advantage of this!

Either way, this is a great article. Way to go Rae. I think The Lisa should let you blog more often if this is the kind of results we’d see. But The Lisa would still need to blog as often as she does too tho!

Reply

34 Lisa Barone 04/21/2009 at 9:08 PM

Alan: I should “let” Rae blog more? Let me reenact the past two months for you:

[clears throat]

Lisa: Hey, Rae. You know what would be awesome?
Rae: Blow me.
Lisa: No, really! You should blog something. Something technical that I can’t cover.
Rae: Blow me, Barone.
Lisa: Please? I think people would really like it. It’d be really valuable.
Rae: You sure you’re not just lazy and trying to get me to do your work.
Lisa: [glare]
Rae: Fine. But don’t bug me. I’ll blog when I feel like it.
Lisa: Yes, Princess.

Reply

35 Alan Bleiweiss 04/21/2009 at 9:13 PM

damn

Reply

36 Jia 04/21/2009 at 9:16 PM

Thanks for the article, I just installed two WP plugins, Link Shortcut from the original post and Synected that was mentioned in the comments. After playing around with them both, I’ve got to say that Synected is much more powerful and well thought out in design.

It basically does everything Link Shortcut does plus:
The click tracking other commenters have asked for.
Allows you to do instant shortening on the fly by sticking the URL after your domain and automatically generating a short domain-branded URL.
Allows you to share URL shortening access from your domain with the public or just with people you trust (through WP admin access).
And a bunch of other security features and tweaks.

Nice work Tyler, please keep updating and supporting this plugin. My only feedback is that there’s a minor UI bug (only bulk moderate works, click delete/block doesn’t work) and I can’t tell what redirect protocol you use. I’m not a SEO person so I don’t care, but it looks like lots of other people prefer 301 or want the ability to control their redirect method.

And if anyone wants to try it out I made a short URL to this page:
http://edeify.com/to/shorturl

Reply

37 Greg 04/22/2009 at 7:24 AM

Redirection for WordPress has 301s built-in, you might all want to have a look: http://urbangiraffe.com/plugins/redirection/

Reply

38 Glen Allsopp 04/22/2009 at 2:40 PM

Great post Rae!

I actually have 2 pretty much full-time PHP developers working for me right now, if you are interested I could send you either of their details. They are on MSN pretty much 24/7 and could probably do this in a few hours.

Reply

39 Husani Oakley 04/22/2009 at 8:29 PM

Hi everyone, I’m the Link Shortcut plugin developer. I’ve released a new version with the ability to modify redirect type, 100% due to this thread. Feel free to download and use … or download and modify and use … or give me suggestions. :) Thanks!

Reply

40 insic 04/23/2009 at 12:15 AM

wow! glad i stumbled this site. this is usefull to me.

Reply

41 Chris Hooley 04/23/2009 at 12:40 AM

This is awesome…. but am I a big jerk for installing this plug and not switching to a 301?

Or better yet, am I a bigger jerk for updating the script to check the outbound URL, and if it’s in an array of URLs I own then 301, else 302?

Reply

42 Gary 04/23/2009 at 1:41 PM

Hey, just a quick “heads up” — I installed that Link Shortcut plugin on a new blog today — and WP immediately gave me an update option. I updated it — and it has the option to select 301 or 302 redirects. Gotta love a plugin writer that responsive!

Reply

43 Rae Hoffman 04/23/2009 at 1:48 PM

Hey guys, the post has been updated to include the function you can add to call the shortened URL on your “tweet this” button/links on the page if one for that page exists!

Reply

44 geobak 04/24/2009 at 5:08 AM

That’s a great post and the update about twitter isa cool too…

I’ve been using pretty links to shorten my urls and cloak my aff links but i think i might give a try to this one too now :)

Thank you

Reply

45 mssmotorrd 05/03/2009 at 7:51 AM

It’s the first time I commented here and I must say you share us genuine, and quality information for bloggers! Good job.
p.s. You have a very good template for your blog. Where did you find it?

Reply

46 Melissa - Mindful Construct 05/03/2009 at 2:09 PM

Thanks Rae, you’re awesome for sharing this! And thanks Husani!

I only get to use 2 random characters at the end of my shortened urls, but it is so worth it to see my domain name pop up. I was getting tired of those tinyurls fast.

Reply

47 Dean Crame 05/04/2009 at 11:47 PM

Wow. Very good information. Thank you.

Reply

48 Blair Williams 05/06/2009 at 11:10 PM

I got fed up with the URL shortening options in WordPress and so I wrote my own plugin that makes this waaaay easier than this process. My plugin is called Pretty Link – you can download it here: http://wordpress.org/extend/pretty-link

You can alter your redirect type from temporary to permanent, forward parameters, generate random slugs (that would keep you well below the 30 char max), adds a nofollow attribute to your links if you want, and allows you to add an optional bar at the top of the screen — kind of like the DiggBar only not quite as evil :) …

In addition to all of this it tracks your links like a nightmare, gives you a great reporting interface and allows you to download the hit data in a spreadsheet. I know I’m biased because I wrote the thing but I’ve been using it in my twitter stream and marketing efforts and it ROCKS!

Reply

49 Lee 05/26/2009 at 3:57 PM

Well, I grabbed copies of both “Link Shortcut” and “Synected”, but I found Link Shortcut had some warnings that kept appearing on its settings page, and Synected doesn’t handle redirects very well when it has to inject stuff into .htaccess (if you specify a blank url prefix). So I went with Pretty Links, by the above commenter.

So far, so good. Pretty links doesn’t barf with non-prefixed redirects, generates a random slug on the fly for you, and keeps awesome stats! I went through and changed the 307′s to 302′s because every so often I see an old HTTP/1.0 UserAgent and I don’t want them getting hung up on the newer 307.

That’s my mini review of the plugins!

@l0gic

Reply

50 Kaila S. 06/05/2009 at 2:18 PM

What a great bit of information Rae, wow. I’m surprised I only saw this now many weeks after the initial post date.

Reply

51 Ralph 06/05/2009 at 6:54 PM

This is great. Thanks for sharing this information and for everyone that commented on their experience. If I ever get around to tweeting more, I’ll definitely give this a try.

Reply

52 Blair Williams 06/17/2009 at 3:59 PM

Lee, I appreciated your comment about the 302 redirect so much that I’ve added a new feature. Pretty Link now gracefully degrades to a 302 redirect from 307 when it sees that the client is HTTP 1.0 … Even though this issue is pretty much moot — (almost all HTTP 1.0 clients are able to understand 307 redirects these days) I thought you’d like to know that Pretty Link behaves in this manor now.

Reply

53 Team Nirvana 08/04/2009 at 8:31 AM

301, 302, 307 I was just wondering where would this end and whether I was getting anything into my grey matter. But, eventually I backfired and researched a bit of the redirects mentioned.

Thanks for the explanation in detail. I am going to use this plugin now and make the changes mentioned.

Reply

54 Sean O 08/13/2009 at 10:21 AM

Great article on using WordPress for short URLs. For those who may or may not be using WP, but are still looking to create a short URL service using their own web site (and their own brand), I’ve recently posted an article on how to do so yourself, in under 50 lines of code:

http://sean-o.com/short-URL

Reply

55 barb hipsman 09/28/2009 at 8:25 PM

It’s not too smart if its owners aren’t using grammar correctly.
it’s v. its….

Reply

56 ashley 10/05/2009 at 4:52 PM

awesommmmmmeeee

Reply

57 Jeff 12/12/2009 at 12:24 PM

I love it when I search and find exactly what I was looking for. Thanks!

Reply

58 Chuck Reynolds 12/15/2009 at 12:04 AM

Nice but now for everybody to find a small domain like is.gd etc

Reply

59 ahsan 01/03/2010 at 1:32 PM

Rae, great article. Precise and to the point. But I have run into problem with this plugin. I know you did not write the plugin but thought you would like to know about it.I installed it on WordPress 2.9 and I keep on getting this warning: footer.inc.php can not be included.
There is no file with this name in any directory. Furthermore, I dont see any Update/Save button on the admin pages. Any ideas?

Reply

60 Malaysia Classifieds 01/13/2010 at 11:49 AM

I’m using Twitter Friendly Links and its works great so far… Just seach for “Twitter Friendly Links” in WordPress Plugins directory and you can get it.

Reply

61 Dave 04/13/2010 at 3:42 PM

Urlshorts.info is a URL shortening service that I’ve created. It still needs tracking implementation.

Reply

62 panah 05/10/2010 at 8:30 PM

Why not use Redirection plugin? Much easier and more versatile. 307 and all kinds of redirects supported.

Reply

63 John Sullivan 05/12/2010 at 2:32 AM

Hi I was going to clown and just ask if your married but maybe next time :)
I’m checking short solutions for a site using limited accessed smarty’s period so I just wanted to say I like your style :)
Thanks very HOT topic

Reply

64 Dan Maas 05/13/2010 at 10:36 PM

Nice article. We will explore this because I have been asking for this kind of tool from my staff for over a year…

Reply

65 Alex 05/16/2010 at 6:11 AM

I prefer http://paidly.com – url shortener. Paidly.com allows you to create a short URL that can be effectively used instead of a long URL.

Reply

66 Christy Correll 05/22/2010 at 3:51 PM

You ladies never cease to amaze me. Thanks for sharing!

Reply

67 Phil Nelson 05/26/2010 at 1:42 PM

Your solution definitely works, but if you’d like a simpler way you can use my WordPress plugin la petite url to accomplish the same function (serving shortened URLs from your own host), with some added advanced features (such as custom shorter domains, which is awesome). It’s free, and it works without having to install any other plugins or having to write a single line of code.

Reply

68 Cory 06/03/2010 at 3:24 PM

Curious as to what the following statement entailed:

“Next, I manually setup a shortened URL the same way I would with Tinyurl.com or any other shortening service in my WordPress backend.”

Slightly vague and I’m lost trying to get past that spot. Did you do this w/ the Link Shortener plugin, another one or via some other tutorial?

Looking for a crumb trail…

Reply

69 Chuck Reynolds 06/21/2010 at 5:16 AM

Can now use the new wordpress function to get shortlinks automatically

http://codex.wordpress.org/Function_Reference/the_shortlink

Reply

70 Rob Woods 06/23/2010 at 8:18 PM

I know this is an older post but I just did the install and it works slick. Thanks again Rae. 90% of the WordPress tricks I know I learned from you or Michael Gray.

Reply

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Want to add a picture to your comments here on Outspoken Media? Upload a picture at Gravatar to make it happen.

By clicking "submit" below, you are agreeing to abide by our comment policy.

Previous post:

Next post: