Custom URL Shortner for your site.

Every want to run your own tinyurl.com-like url shortner where you controlled EVERYTHING (including the URL itself.?  We are going to create a new domain and forward that domain to the location to a script you own somewhere on the Internet.  It could theoretically be a blogger page if you'd like.  The final location of the shortner will be accessed like this:

http://x.com/blog (if you owned x.com) which can redirect to any place on the internet.  You should be able to replace blog with the keys to all of the URLs you have set up in the script.

Create The Script On Your Web Server
On a server located on a different domain from the one you just registered. Put the following in the root of your web server, and call it something like u.html.  The name and location is unimportant.  You can verify that it works by surfing directly to it.  It should show you a list of your links and allow you to click on them to go to them.
<html> <script language="javascript"> function getParameterByName(name){ name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); var regexS = "[\\?&]" + name + "=([^&#]*)"; var regex = new RegExp(regexS); var results = regex.exec(window.location.href); if(results == null) return ""; else return decodeURIComponent(results[1].replace(/\+/g, " ")); } var key =getParameterByName('q'); var urls={ '/delicious':'http://www.delicious.com/davidron', '':"http://blog.davidron.com", '/':"http://blog.davidron.com" } if(urls[key]){ window.location.href=urls[key] }else{ document.write("'"+key+"' not found :("); } for(urlKey in urls){ document.write("<br /><a href=?q="+urlKey+">"+urlKey+"</a>"); } </script> </html>

Point A Custom Domain Name At Your Script
Create a subdomain on your web site that redirects to the url of the script you created.  In godaddy, I did the following:
  • Log into godaddy.com
  • click domains->domain management
  • Click the name of your domain
  • At the bottom of the domain information section, click "manage".
  • Forward it to http://thelocationofyourscript.com/u.html?q=
  • Make sure you add the ?q= part to the end, that's the part that translates your shortened URL to a query in your script.

Add More URLs

To add more URLs, just edit the script and edit the block of code that looks like this.  Each custom URL has to start with a "/".  The two special cases are when the user goes to your shortner without specifying a custom part to the URL, those are the last two in the block (the empty and the plain / cases):
var urls={
    '/delicious':'http://www.delicious.com/davidron',
    '':"http://blog.davidron.com",
    '/':"http://blog.davidron.com"
}

No comments

Post a Comment