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



Most people use URL shortening services like Bit.ly, TinyURL, Awe.sm, Su.pr, 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, Cli.gs announced it will shutdown on October 25. Tr.im 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 301works.org, 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.

Requirements:

  • 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:

http://www.nerdgrind.com/

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 domai.nr 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 ngurl.me 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.

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

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

mysql -u root -pyourpassword
create 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 http://yoursite.com/admin/ and you should see the following page if everything is setup correctly.

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

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

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

Now all you need to do is login.

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

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.

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

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:

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

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 ngurl.me with your own domain name in lines 3 and 4.

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

Let me know how YOURLS works for you.


Short Link - http://ngurl.me/p Posted on October 8, 2009 at 10:46 pm (PST)
Tags: | | | | | | | | | | | | | | |


8 comments on “How To Create YOURLS Own URL Shortener Service with Your Domain Name

  1. Trishah on said:

    I am trying to install Yourls for the first time and the config page keeps getting rejected. I’ve installed hundreds of programs and never had this problem before! I can’t tell you how many times I’ve created new config pages, reuploaded everything and started again. The error message I’m getting follows… Any help you can give is greatly appreciated!

    YOURLS: Your Own URL Shortener
    YOURLS

    Your config.php does not contain all the required constant definitions.

    Please check config-sample.php and update your config accordingly, there are new stuffs!

    Powered by YOURLS v1.4.3 – query

  2. Trishah on said:

    Thank you for the tip. Unfortunately I looked through all of what is on the site and there was nothing that helped. I have written one of the creators for help. We’ll see what happens there. Until then, I have installed urlshort which I had no problem installing. If I discover what went wrong with Yourls I will definitely update here.

  3. a new guy on said:

    This information helps. however, I still cannot find the answerI need. Hopefully some experts can help me here. I have successfully installed YOURLS under my root directory, but I want to create a new page to allow users to use this servive such as pli.gs or s4c.in. Does anyone know how do I implement this? Should I copy the code from the admin page and modify it? Thanks!

  4. StratosJL on said:

    Hello, I have set up yourls, but keep getting 404 not found… what should i be looking at?

  5. EsLocoHQ on said:

    Has any found a way to get around a wordpress install and a YOURLS install into the same directory or perhaps a way to redirect the wordpress site to domain.com so domain.com can also be used for YOURLS

    • StratosJL on said:

      I have managed to have YOURLS running on the same domain as a wp plugin, by setting it up on a subdomain! Check it out:

      http://url.laspas.gr/20

  6. marco on said:

    i cant’ seem to make it work. all seems fine, it does write on my db but then i always get a 404

Leave a Reply

Your email address will not be published. Required fields are marked *

*

70,029 Spam Comments Blocked so far by Spam Free Wordpress

HTML tags are not allowed.

© Copyright Nerd Grind 2009 - 2010. All rights reserved.