Setting up a URL Shortener with YOURLS

user-pic
Vote 0  

Filed:

 
yourls-logo.gifRight about the time Bit.ly Pro was announced and they were taking invites, I wanted to setup our own URL shortener. Short URLs are very important in the Twitter World, and brand recognition is also important. As it turns out we finally got our Bit.ly Pro invite about a month and a half after setting up YOURLS. And frankly, I would rather have complete control anyways.
Step 1. Find a great short URL. I am pretty proud of the domain hack I found, http://bgne.ws/. I later noticed that the AP is using something similar, what a coincidence: apne.ws. There are a number of places to look but Domainr is one of the best. It searches TLDs as well as other hacks such as using folders or subdomains. With so many TLD extensions out there these days, you should be able to find a creative use of your brand name.

Step 2. Install YOURLS. See the Install page for details. It is pretty much fill in the blanks in the config.php file, something you should be used to by now. Right?

Some advice:

define('YOURLS_UNIQUE_URLS', true); Set this to true so that your system does not churn out 10 short links for 1 article. If someone tries to shorten something that has already been shortened, it just returns the existing short URL.

define('YOURLS_PRIVATE', true); I don't recommend setting up a public install of YOURLS, that is just asking for trouble.

$yourls_user_passwords In this array make sure to create at least one username/password set that has special characters (just use alphanumeric). This is so when you want to use your YOURLS API with TweetDeck, Twitter for iPhone, or any number of other Twitter services, you don't have a headache.

.htaccess Tip: You cannot use YOURLS in the same directory as other apps that make heavy use of URL rewrites, such as WordPress. Best to run YOURLS in as clean an environment as possible.

Step 3. Make the Home Page look spiffy. Obviously optional, but it took little time to change the default home page into our site's shell.

At this point you are all setup. You can now create short URLs yourself, even use the Bit.ly standard of adding a + at the end of the URL to see the stats (such as here). You can automate the creation of these short links for WordPress, and now Movable Type, and you can even use the API for your own YOURLS install.

If you are not using WordPress or Movable Type, and can't find a plugin for your needs, you can still make easy use of the API. Read this Gist. The only problem with that is that it would run every time the page was accessed, and it does not store the Short URL in a database. You would want to add the following to your config.php to disable flood blocking:

define('YOURLS_FLOOD_DELAY_SECONDS', 0);

There is a better way to handle this in Movable Type, Twitter Tools. I will cover that in a future post.

Third Party Tools

Lets setup YOURLS so you can easily use it with some third party tools. I will be using TweetDeck and Twitter for iPhone (Tweetie 2) as examples.

Step 1. Duplicate yourls-api.php and give it a unique name, something short. Edit this new file and delete the Line yourls_maybe_require_auth(); (this should be Line 4). This allows you to pass options to your API and not require a login. Typing the URL on a mobile device is hard enough as it is.

Using YOURLS with TweetDeck

In Preferences, go to Services and change the Short URL service from Bit.ly (or whatever it is currently set to) to Other. Now use the following URL as a template, modify the domain and the filename to the API file you just duplicated:

http://domain.com/filename.php?action=shorturl&format=simple&url=%@

Now pasting URLs into TweetDeck will use our own URL Shortening service.

Using YOURLS with Twitter for iPhone

Also known as Tweetie 2 before Twitter bought it. Go to the "..." button in the bottom toolbar and Accounts & Settings. Click on Settings, Services, URL Shortening. Select custom and very carefully type in the same URL used above, then hit Save.

http://domain.com/filename.php?action=shorturl&url=%@&format=simple

When you paste a URL into the New Tweet menu touch the little icon that displays how many characters you have remaining, and touch the Shrink URLs button. It will now shorten them using your YOURLS install!

Since we don't use WordPress, I can't speak for how easy it is to use that plugin, but it looks pretty strait forward. In my next post I will go over how to use Twitter Tools for Movable Type with YOURLS, which is what we do have setup for The BG News.

Email to a friend

Anonymous comments are always moderated, please sign in and join the discussion. Create an account for a user profile and community features. Please read our comments policy.

Leave a comment

Poll

Do you like the toolbar at the bottom of this blog?

View results

View & Download PDF


View our support community!

About This Blog

Ken Edwards (and hopefully other project members) will be blogging here — a place to keep up on what is happening on the BG Views project. You will find bits of code, project updates, and random technical information aimed at confusing the hell out of you. More »

Subscriptions

Mobile Edition

Mobile EditionAccess the BG Views Developer Blog on your iPhone or other mobile device. Read more about our mobile site »
Powered by Movable Type 4.34-en

 

Also powered by:

  • Caffeine (Dr. Pepper)
  • Cup Cakes (Hostess)
  • Pizza (Pisanello's)