How to set up “Your Own URL Shortener” (YOURLS)

Share on Facebook2Share on Google+4Tweet about this on Twitter4Share on LinkedIn0Share on Reddit0Share on Tumblr

Keeping up appearances

There was something that annoyed me for some time: whenever I tried to copy-paste an Internet address written in Greek, the copied text would not appear in Greek; it would appear in some weird coding, where non-Latin characters were replaced by the ASCI code equivalent of the characters. That would (a) take up too much space and (b) appear gibberish and no-one could tell what it meant.

For example, what do you make of the following mess?



Looking for the solution

I first thought that I could fix that by changing something in the way that address text was stored in the blog’s database. But that dealt only with the appearance of the actual Greek characters in permalinks on the web page. Apparently, even if displayed right, when you try to copy-paste them, you get the above cryptic code.

This has to do not only with the core code of WordPress, but also with the way that sites, web servers, Internet browsers and plugins interpret & encode URLs. Well, what can you do, apparently the Internet is not fully internationalized yet

On the other hand, a cool feature that I always liked, was the short-URL service offered by such sites as TinyURL. Because, even post links (permalinks) written in Latin characters, are difficult to handle or refer to, if they are particularly long.

The above two reasons combined, that is, the need for a handy way to refer to my posts with Greek permalinks and the need for a short link service in general, sent me searching for a way to integrate a URL shortener service into the blog itself.

Enter “Your Own URL Shortener” or Y-O-URL-S!

Your Own URL Shortener

Your Own URL Shortener

What is YOURLS

YOURLS is a small set of PHP scripts that will allow you to run your own URL shortening service (a la TinyURL). You can make it private or public, you can pick custom keyword URLs, it comes with its own API. You will love it.

There’s a WordPress plugin available for YOURLS, making integration with your blog a snap: create short URLs and tweet them automagically as you publish blog posts.

After setting this up, the messed-up-looking permalink in the example above would look something like this:

It may still not look quite as good as having the original, meaningfull Greek web address, but at least it’s much more manageable and easy to copy-paste!

Installation is very straightforward, following the directions at the project’s site:
  • Unzip the YOURLS archive
  • Copy includes/config-sample.php to user/config.php
  • Open user/config.php with a raw text editor (like Notepad) and fill in the required settings
  • Upload the unzipped files to your domain public_html or www folder
  • Create a new database (see Configuration – you can also use an existing one)
  • Point your browser to

After that, you’ll have to install two plugins for WordPress:

I was personally interested in the second one, so that a short URL would appear automatically alongside each post or page; but it depended on the first one, so…

Fine tuning for use on single domain

The process to set up the above plugins is described in their relative pages. My problem was that, although I found very detailed instructions on setting everything up, I could not install YOURLS so that it would reside in my own web sever, alongside WordPress, in the same domain. Apparently, it had to do with this note on their site:

A note to WordPress users

You cannot run YOURLS and WordPress in the same directory (ie they cannot share the same “.htaccess” file). Refer to the readme file for more details

Directory setup

After a few days of experimentation, here is how it actually worked.

  • From my domain’s control center (cPanel), I set up a new subdomain:
  • I installed YOURLS in the subdomain’s root folder, public_html/url
  • I set up YOURLS as if it resided on a different host
  • I set up “YOURLS: WordPress to Twitter” as if it resided on the same host
  • I played around with the .htaccess files

.htaccess and config file setup

The .htaccess file within public_html/url is as follows. You’ll notice that it also redirects the subdomain to the main domain, so as not to lose SEO points Wink:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /yourls-loader.php [L]

RewriteCond %{HTTP_HOST} ^$ [OR]
RewriteCond %{HTTP_HOST} ^$
RewriteRule ^/?$ "" [R=301,L]

In the config file of YOURLS, the only thing that differs from a standard installation, is that it points to the new subdomain:

/** YOURLS installation URL, no trailing slash */
define( 'YOURLS_SITE', '' );

The “YOURS: WordPress to Twitter” plugin is setup “locally”. I don’t know why, but I kept getting a “wrong password” message when I tried to set it up via the API, pointing to the subdomain.

YOURLS-WordPress to Twitter

YOURLS-WordPress to Twitter (click to enlarge)

On the other hand, the “YOURLS: Short URL Widget” was pretty straightforward to set up.

YOURLS-Short URL Widget

YOURLS-Short URL Widget (click to enlarge)

Now the short URL widget appears on the right column for each individual post or page. Not on the front page, however, and that’s a feature, not a bug!

This will work for all new posts and pages. However, note that for older posts, you may have to go to the post, enter edit mode, then click on the “Reset short URL” button, in the YOURLS box on the right and finally update the post.

Share on Facebook2Share on Google+4Tweet about this on Twitter4Share on LinkedIn0Share on Reddit0Share on Tumblr

4 thoughts on “How to set up “Your Own URL Shortener” (YOURLS)

  1. Thanks! This saved me a lot of trouble, since I have really no idea what I’m doing with the .htaccess file. I copied yours, modified it, and can sleep now.

Share your thoughts! - Πείτε μας τη γνώμη σας!