More URL Shortener Ahso.me-ness
I've added a little more URL shortener ahso.me-ness to my stock URL shortener. I posted about this awhile back and this is an update to that code.
If you have questions about URL shorteners, see that post. They are easy to create. Really, really easy. That URL shortener is the easiest functional program I've ever developed.
My work is using a copy of that code to serve two company short URL domains because we didn't want to run the risk of a tr.im style shutdown. My "play" copy of the shortener is at wjmp.net -- a domain I registered back when ABC and T9 were the main ways people typed on phones (before the iPhone, etc). w,j,m,p are all the first letters in their respective numbers. You wouldn't have to keep punching the numbers to get to it, see?
So the shortener works like all the other ones, but I added a small feature.
In WordPress, each post has an ID. This post is ID 1603. To get to it, you can type http://ha17.com/?p=1603. It will redirect you to the "pretty", "SEO" version but it's a great way to make it "short". I've been using it just like that. I don't have to send an API request to any shortener service, just load the post and this:
$site_url = 'http://' . SHORTENER . '/-' . get_the_id();
The $site_url used to look like this, BTW:
$site_url = 'http://' . SHORTENER . '/?p=' . get_the_id();
Why bother? Well, twitter sometimes RE-shortens my URL for me if, I'm guessing, it has a ? in it. Plus, I like the "shortener" look better, especially if it's branded for my web site. And, I won't have to worry about it not working, unless I shut down my shortener, the link lives on as long as I want. At least, I control that part of it. If I wanted, I could password protect the "adding" part and only allow URLs I want to shorten. If you have a web site built on trust, this is a way to tell your users, "It's safe to click on a shortened URL used with my personal shortener."
Here's the additional code. I check the first character for a '-' if it exists, it's a WordPress ID. So I strip off the dash and redirect to that post. I don't even hit the database.
If that part of the URL does not start with a dash, I proceed to the part that decodes the string, checks the database and redirects.
if ('-' == substr($url,0,1)) // this is a "wordpress short url"
{
$host = $_SERVER['HTTP_HOST'];
switch ($host)
{
case 'ahso.me':
redirect_wp('annieology.com',$url);
break;
case 'ha17.us':
case 'ha17.me':
redirect_wp('ha17.com',$url);
break;
}
}
function redirect_wp ($host, $url)
{
$url = substr($url,1);
header("Location: http://{$host}/?p=$url");
exit;
}
See that Twitter icon down in the left there? Click that and you'll see this WP-shortener in action.
