Short URL services have become a critical part of sharing links around the web. These services take a long URL like https://www.markhodder.com/2011/02/create-your-own-short-urls/ and convert it to a smaller URL like http://mah.lt/s/6
Both links point to the same source, one just has fewer characters (17 vs 53)
These shorter links save precious space, which is critical when it comes to sharing them on Twitter. More then just making a link shorter, these services can also collect additional data, every time the link is clicked.
Information like how many people clicked the link, the countries they visited from and the source where the link was clicked. They even make it easy to update a link after it’s been shared. This allows you to point to a new source if the content has moved.
Why not use an Existing Service?
There are plenty of companies out there offering this service for free, or for a small fee so why take the time to create your own URL shortner?
The most obvious reason is that you are at the mercy of the companies you create your links with, if they are down so are your links. Even worse, if the company closes down your links could be gone forever.
Another great reason to create your own service is that you can create a more personal link. Instead of promoting a 3rd party company every time you share a link, you can be promoting your own brand.
Finally with some free tools, it’s relatively easy to get setup and creating your own URLs in no time!
The first thing you need to decide is if you want to use an existing domain, or if you want to purchase a dedicated domain to shorten URLS. A case can be made for either side, personally I choose to register a unique domain.
I did this so I could create the shortest possible links and so I could use the same domain across multiple sites without it looking weird. I registered my domain mah.lt (it’s my initials) at gandi.net/domain – I recommend them for their large choice of international domains, which allows you to come up with some creative short names.
What else you’ll need:
- If you elected to purchase a new domain, you’ll also need to have it setup with your web host. The server should have at least PHP 4.3, at least MYSQL 4.1 and mod_rewrite enabled. If you run into problems setting things up you might want to check with your web host to make sure you meet these requirements.
- The latest version of the free YOURLS software at yourls.org
- A MySQL database and connection information for that database (you can get this from your web host if you don’t know it)
Once you have your domain and web hosting setup the first thing you want to do is setup YOURLS.
Download the latest version of YOURLS (version 1.5 as of this writing) and unzip it to your computer. Copy the includes/config-sample.php to user/config.php and open it with your favorite raw text editor (like Notepad) or HTML editor. Fill in the MySQL settings by changing dbuser, dbpassword, yourls, localhost to your MySQL settings (you can get this from your web host if you don’t know them)
Now scroll down to the Site Options area and change http://site.com to the URL you plan on uploading the software to (in my case http://mah.lt/s)
Visit http://yourls.org/cookie and copy the line displayed and replace the define( ‘YOURLS_COOKIEKEY‘… line in the config file.
Finally change ‘username’ and ‘password’ to your desired admin login, if you only need one login delete the extra line: “, ‘username2’ => ‘password2’” (or you can use this to create multiple users) – now save config.php
Upload all the YOURLS files to the domain you want to use to create the short links. YOURLS recommends uploading the files to the root of your site, however, if you’re uploading YOURLS to an existing site (with WordPress running for example) you want to make sure to upload the files to sub-folder (ie http://mah.lt/s – I created a new folder called “s” you can use whatever you want) and not directly to your root folder. This will prevent any conflicts with your existing site and the YOURLS code.
Now you should be able to load the the /admin folder (ie http://yoursite.com/s/admin/) to finish the YOURLS setup. You can login and begin creating your own personal short links using the easy to use web panel!
Bonus: Integrating with WordPress and Twitter
To create an even more powerful solution you can integrate your local or remote YOURLS setup with your WordPress site and SexyBookmarks plugin to let users share your new short links with Twitter automatically.
What you need:
- The YOURLS WordPress to Twitter plugin (at least PHP 5 needed to run this plugin)
- The SexyBookmarks plugin for WordPress
Install and activate the two plugins listed above. Then visit the settings page for the YOURLS plugin which is located at Settings -> YOURLS
Under URL Shortener Service select that you are using your own YOURLS install. If you installed YOURLS on a separate domain from your WordPress site you need to select the remote option in the next drop-down otherwise select local. Fill in the location to the yourls-api.php file, if you selected the remote option enter your YOURLS login / password in the boxes provided. Make sure everything is correct by clicking the check button.
Now hit the + button to drop down the Twitter settings. You’ll need to register your WordPress site with Twitter read the instructions provided in this section to get the information you need and fill in the boxes.
Finally click the + button to drop down the WordPress settings and select Generate a short URL under “New Post published”. If you want to automatically Tweet when you post a new blog posting click the “Send a tweet with the short URL” box as well. You can customize how the tweet will look under the “Tweet message” section.
Now every time you create a new blog post it will automatically create a corresponding short URL using your YOURLS service and optionally post it to Twitter for you!
By linking our YOURLS service and the popular SexyBookmarks plugin we can also let others share our content with Twitter using our short URLS automatically.
To do this go to SexyBookmark settings under “Shareaholic -> SexyBookmarks” and look for the “Which URL Shortener?” drop-down. Select YOURLS from the drop-down and save your settings and you’re ready to go.