How To Create Your Own URL Shortener With YOURLS
Written by Jon Henshaw and published
Earlier this year, I wrote about why link shorteners matter for content marketing. In the article, I mentioned how there’s no longer a need to shorten links for social networks like Twitter, but there’s still a need to shorten them in order to get campaign insights using Campaign Variables.
The most widely used URL shortener is Bitly, but if you want to use your own domain and be able to fully customize the URLs, you’re going to have to pay at least $995/mo. While you get some nice metrics with their service, you should be able to get all or most of the metrics you need from Google Analytics as long as you’re using Campaign Variables.
Fortunately, thanks to the open source project YOURLS, creating and hosting your own URL shortener has never been easier.
Picking Your Domain
Many people think you have to get the shortest domain possible for shortening URLs, but that’s not true. While it may be nice to have a super short domain, all you’re really looking for is a domain that is related to your brand. For example, raven.link is a perfectly good domain for shortening a long URL with Campaign Variables. This long URL:
…looks and works perfectly well as /blog/ssl-wordpress-cloudfront/.
Installing YOURLS is about as easy as installing WordPress. Also, if your Web server can host WordPress, then it should be able to host YOURLS without any problems.
Step 1: Download YOURLS
YOURLS is open source and the latest version can be downloaded at GitHub.
Step 2: Copy Files
Copy all of the files into the root folder for the domain you’re using. Then create a blank .htaccess file and then change the file permissions (chmod) to 777. When you run the YOURLS installation, it will need to be able to write to it. After the installation, you can change it back to 644.
Step 3: Create Database
Create your MySQL database and save the username and password (with full permissions), database name and database server name, which will be used in the next step.
Step 4: Configure YOURLS
In the /user folder, rename config-sample.php to config.php. Open the file in a text editor and do the following:
- Enter the database’s username, password, name and server
- Enter your site’s URL
define( 'YOURLS_SITE', 'http://yourshortdomain.com' );
- Replace the “modify this text…” with random text of your own
define( 'YOURLS_COOKIEKEY', 'modify this text with something random' );
- Replace the username and password with the username and password you will use to access your YOURLS installation. If you’re only creating one user account, be sure to remove username2 and password2 line.
Save the file and then change the file permission (chmod) to 777. When you run the YOURLS installation, it will write to config.php and change the plain text password into encrypted passwords. After the installation, you can change it back to 644.
Step 5: Run the installation
In your browser, visit yourshortdomain.com/admin. The installation will run and then you’ll be able to login to your custom YOURLS account.
Important: Before you proceed, you need to change the file permissions (chmod) to 644 for the .htaccess and /user/config.php files.
Step 6: Redirect root domain to main site
This is an optional step, but I prefer to have the root short domain redirect to my main marketing site. For example, if someone visits http://raven.link I want them to be redirected to http://raventools.com. Typically, I would place the 301 redirect in the .htaccess file, but that’s used by YOURLS to redirect shortened links. The best alternative is to replace all of the HTML in index.php with the following PHP script:
header("HTTP/1.1 301 Moved Permanently");
Adding Campaign Variables to Shortened URLs
There are several way to add Campaign Variables to your shortened URLs.
GA Config URL Builder
You can use our URL Builder to manually create URLs with campaign variables, and then copy and paste it into YOURLS.
Automating in Raven
Raven supports both YOURLS and applying Campaign Variables automatically to your shortened links.
In Raven, go to the Twitter Marketing Tool and then click on Tools Options > Customize Settings. Choose Custom for the URL Shortening Service. Next, enter the following link to enable Raven to shorten links with your YOURLS account. Replace yourdomain.com with your own and 1111111111 with your YOURLS secret signature token. You can retrieve your token from the Tools section of your YOURLS admin.
Now when you post or schedule a tweet from Raven, you can shorten the URL using your custom YOURLS domain.
The Future of YOURLS
YOURLS is actively developed by Ozh Richard. I had the opportunity to interview him earlier this year about YOURLS and its future. Here’s what he had to say:
Jon: Where did the idea for YOURLS come from, and what made you decide to build it?
Ozh: The idea of running my own personal URL shortener sprung to my mind in April 2009. A couple months earlier I had built something quite similar (scr.im, a little service to convert your email into a short URL before sharing it on social sites, forums et. al., still running but sold since) and I thought it would be cool to run my own URL shortener.
I don’t think there are hosted solutions such as today’s Bitly available currently, so this provided further incentive to code my own stuff.
Jon: The next version (1.7) is almost available and includes some nice improvements and important security updates. Do you see the YOURLS‘ development slowing down or are there some big updates we should expect to see in the future?
Ozh: YOURLS 1.7 was released a couple days ago. I’m currently publishing a series of posts on the YOURLS blog to highlight new features.
YOURLS dev cycle is quite slow (this is mostly a one man show and free time is rare), but the dev is definitely not slowing down. We have tons and tons of ideas for future features, and definitely not enough free time to implement them. YOURLS road map has enough to keep us busy for the next 10 years (no kidding). Check it at https://github.com/YOURLS/YOURLS/wiki/Road-Map
The next version, 1.8, should feature a cron job emulation, which will allow to perform more easily automated tasks (DB cleaning, checking for versions, etc.).
The next big version, 2.0, will feature a completely revamped interface, the ability to design custom admin themes, and a new DB schema that will drastically improve YOURLS‘ flexibility. I plan to release several plugins to show off this.
What’s exciting is that 2.0 has been in the works for almost a year already, in parallel dev to the main dev cycle, so we have plenty of things to be excited about.
As always and noted on the road map: no ETA. Never 🙂
Jon: You do a lot of development in PHP and WordPress. What are some of the other projects you’re currently working on beside YOURLS?
Ozh: I used to be quite active in the WordPress plugin world: I released numerous plugins, totaling more than a million downloads, and I published a book on that matter which is still the best reference for WordPress plugin coding available to date.
But, as I’ve said above, free time is rare, and I just don’t have enough for all the projects I would love to work on. For the last couple of years, YOURLS has been my main project, and I think it will stay this way for the years to come.