How To Create YOURLS Own URL Shortener Service with Your Domain Name

Most people use URL shortening services like, TinyURL,,, and the list goes on. There has been a lot of concern over the long term stability of these services, since they aren’t profitable business models, so they can’t be relied on over the long haul. One popular service, announced it will shutdown on October 25. sudently shutdown, and then reopened just as suddenly, so it’s hard to trust short URL services. One effort will be to provide an independent directory service for URL mappings through, but that isn’t going to solve the problem of reliability.
Sometimes the only way to solve a problem is to do it yourself. I’m talking about setting up your own URL shortening service using YOURLS: Your Own URL Shortener, which is a set of PHP scripts written by Lester Chan and Ozh Richard. Let’s go through what YOURLS offers and how to set it up.

YOURLS features include:

Public (everybody can create short links) or private (your links only)
Sequential or custom URL keyword
Handy bookmarklet to easily shorten and share links
Awesome stats: historical click reports, referrers tracking, visitors geo-location
Neat AJAXed interface
Developer API
Friendly installer

In the next version there will also be charts for statistical data. The best feature is not having to rely on any other service except your own. The second best feature is setting up YOURLS is as easy as installing WordPress.
YOURS also has a WordPress plugin to make it easy to integrate YOURLS into your WordPress blog.

A server with mod_rewrite enabled
At least PHP 4.3
At least MYSQL 4.1

Let’s get started setting up the ULR shortening service using YOURLS.
1. Register a short domain name.
It’s important to know that the maximum number of characters for a URL that Twitter will display is 30. If we used our domain name:

That would total 25 characters. It’s best to choose a short domain name, and leave off the www. at the start to save another 4 characters.
A service like can help find a domain name easier than using a regular whois.
2. Get a web hosting account
YOURLS cannot be installed and run in the same directory as WordPress, so it must be setup in an account of its own, that supports PHP 4.3 or above, and has MYSQL 4.1 or above.
3. Download YOURLS
4. Configure YOURLS
The configuration file is located in include/config.php. If you only see the config-sample.php file, then rename it to config.php. The config file is self explanatory. The part that says YOURLS_SITE is where you put your newly registered domain name. NG chose for our domain name we’ll be using for our shortened URLs. For the encrypted cookie key we suggest using the WordPress key generator for a more secure key. Refreshing the page generates a new set of keys. Ignore the labels, just grab any key since they are just long strings.
In the config file you can choose private or public. There’s no money in having a public URL shortening service, so choose wisely.
5. Upload the YOURLS files and directories to your web host account.
6. Create database
You can use your control panel to create the database or the command line.

To create the database from the command line use the following:

mysql -u root -pyourpasswordcreate database yoururlshortener;grant all on yoururlshortener.* to ’username’@’localhost’ identified by ’password’;show grants for username;quit

7. Install and Setup the Database
Point your web browser to and you should see the following page if everything is setup correctly.

Once you click the Install YOURLS button the script will create the tables and fields in the database.

Now all you need to do is login.

The admin page lists the original URLs, Short URLs, Clicks, and more. A couple of great features are located in the Tools page. The next version will also have graphs, and a lot more features.

8. Backup the Database
There is no automated backup for the YOURLS database, yet, so we’ll need to setup our own. The easiest way is to setup a cron job that overwrites the backed up database file every day by using the following command:

/usr/bin/mysqldump –add-drop-table -h localhost -u username -ppassword database | /bin/gzip -c > /var/www/yoursite/backup/database.sql.gz

There are a lot more backup solutions here, here, and here.
9. Install WordPress YOURLS plugin.
Below is a snapshot of how we configured our YOURLS WordPress plugin:

10. Force URL without the WWW
By removing the www. from the URL you will eliminate 4 characters, which could be needed since Twitter only allows 30 characters total in a shortened URL. To do so add the following lines to the top of your .htaccess file, but of course replace with your own domain name in lines 3 and 4.

# BEGIN Without WWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]
# END Without WWW

Let me know how YOURLS works for you.

Leave a Comment