Generating Root Short URLS From A YOURLS Subdirectory Install

With my increased use of Twitter I find myself tweeting links to articles quite often. But when space is limited to 120 characters and URLs are long one runs out of space to add the link quite easily. The standard way around this is to use an URL shortening service and post shortened URLs. I’ve used one of the more popular URL shorteners, bit.ly, for the last couple of years.

Though I was satisfied with bit.ly it always bothered me to be dependant on a service. What would happen to my links if the service went away? Or if their Libyan top level domain (.ly) was taken away? All my posted URLs would no longer work! So I made the decision a while ago to use my own URL shortener so I have complete control. And a couple of weeks ago I finally got around to implementing it.

The shortner I’m using is YOURLS. It was quite easy to install and is well documented in many other places (most notably, perhaps, on Lifehacker) so I won’t go into details about that process here.

Though getting started was simple I still had some issues. The most notable was based on my preference to segregate apps on my web space. I wanted all of the files from YOURLS to be placed in a subdirectory (so I wouldn’t accidentally remove required files from the root accidentally while cleaning up). But that lead to short URLs that included the subdirectory of the install. Who wants URLs like golddave.com/subdir/xxx? golddave.com/xxx is so much more convenient as it uses fewer characters.

The answer to this dilema was to change the .htaccess file in the root to add the following rewrite rule:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9A-Za-z]+)/?$ /subdir/yourls-go.php?id=$1 [L]
RewriteRule ^([0-9A-Za-z]+)\+/?$ /subdir/yourls-infos.php?id=$1 [L]
RewriteRule ^([0-9A-Za-z]+)\+all/?$ /subdir/yourls-infos.php?id=$1&all=1 [L]

With this rule in place golddave.com/xxx would be the same as using golddave.com/subdir/xxx.

This is a nice, simple solution and works great but there’s still a problem. YOURLS still generated short URLs with the subdirectory so I had to manually edit the short URL before pasting it to where I wanted to use it. This annoyance would also affect URLs shortened by my Twitter client (which I had set up to use YOURLS as it’s URL shortener). (The instructions here are for Tweetdeck but are similar for other clients.)

This was an annoyance I didn’t want to deal with every time I wanted to post a link so I set about reviweing the YOURLS PHP code in the hopes of finding a way to have YOURLS generate short URLs with the subdirectory already removed. And I found it.

The real meat of YOURLS is functions.php which can be found in the includes directory of the YOURLS installation. I found that functions.php uses a setting from the YOURLS config file called YOURLS_SITE, to return the short URL of a given link and other various related functionality. (This setting is manually set when installing YOURLS.) I simply replaced YOURLS_SITE with a substring of YOURLS_SITE in all but two functions that use it (yourls_admin_url() and yourls_site_url() functions).

For our purposes let’s say that YOURLS_SITE is set to “http://example.com/subdir” (where “subdir” is the name of the directory where YOURLS is installed). Substitute the following code every time YOURLS_SITE is mentioned (except for the two functions mentioned at the end of the previous paragraph):

substr(YOURLS_SITE, 0, 18);

This essentially replaces “http://example.com/subdir” with “http://example.com” (the first 18 characters of “http://example.com/subdir”). The number 18 will be different depending on the length of your domain name (for instance, “http://golddave.com” is 19 characters so I used the number 19).

Now I have my YOURLS installation in it’s own subdirectory but am able to generate and use short URLs from the root.

Tags:

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.