Running YOURLs API from a Subdirectory Install
Some time ago I wrote about how to generate root short URLS from a YOURLs subdirectory install. For example, if you have YOURLs installed in the subdirectory ‘subdir’, by default your short URLs will come out as example.com/subdir/xxx. But example.com/xxx is so much nicer. The post details exactly how to get the nicer URLs.
But recently I wanted to use the YOURLs API to expand a URL and it was failing. After some digging, I figured out why and how to fix it.
In the includes directory (yourls/includes) you will find a file called functions-api.php. This file uses the same YOURLS_SITE setting we saw in the previous post. We just need to replace it the same way we replaced it the last time.
Just as last time, 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 (at the time of this writing there are only 3 occurrences):
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 when you try something like http://example.com/yourls/yourls-api.php?signature=SIGNATURE&action=expand&shorturl=http://example.com/KEYWORD&format=simple (where SIGNATURE is your secret signature token and KEYWORD is the keyword to the short URL) you will get the actual long URL instead of a 404.
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.