blog

Own domain URL shortening, with custom short URLs and root redirection, using Bitly

Know your stuff? Skip to code for .htaccess.

This post is for people who have a short domain (e.g. http://sho.rt) and want to use it as a URL shortener but haven’t started anything yet or have it set up using Bitly but want to redirect the root of the short domain to your own site.

If you’re already redirecting the root using .htaccess but want to create custom short URLs, see this post.

Thanks to Blinktag for their post on how to redirect your custom short domain’s root when using bit.ly.

You’ve seen the pros do it (on.fb.me / on.mash.to / goo.gl), and now you want your own URL shortener. Something snappy that shows off your technical ability and looks great in a Tweet like: jam.es or stev.ie.

Well, thanks to bit.ly, you can now do it for free (+ the cost of your domain & hosting (you can get free hosting from companies such as http://www.000webhost.com/), which I assume you have already).

Click settings, then Add a Custom Short DomainYou need to go to bit.ly and create an account. Once that’s done, sign in to your new account and go to “Settings” (click on your username, upper right corner). Click on “Add Custom Short Domain” under “Custom Short Domain”.

For this bit, you’ll need to edit the DNS records with your hosting or domain provider. Follow the instructions in Bitly (give it your short domain) and set the domain’s A record to point to the IP address Bitly gives you (SAVE THE ORIGINAL IP ADDRESS, YOU WILL RESTORE IT LATER).

Don’t logout because you’re going to change this back once it’s done. Bear in mind that this can take a while (up to 24 hours) so if it can’t verify it the first few times, leave it for a few hours. Also, note that you only need to change the main A record to your site (not mail.site.com or ftp.site.com).

When bit.ly is verifying your short domain (this may be instant, otherwise it will ask you to check back in a  few minutes), it’s time to setup your .htaccess (what’s that?) file. It must reside in the root of your site for this to work. It won’t work with .htaccess files in subdirectories.

There are three parts to this:

  1. The custom URLs. Bitly is full of custom URLs, like bit.ly/JacksPhotos instead of bit.ly/ngHD8. Ordinarily, using your own domain with Bitly wouldn’t allow for custom URLs unless they were still available on bit.ly, but most are taken. However, because we’re using .htaccess and not Bitly’s method, we can create whatever custom short URLs we want. The best bit is, we can recycle them.
  2. Root redirection. Another caveat of using Bitly’s recommended method is that all traffic to the root of your short domain goes straight to http://bit.ly. However, yet again we can tell this to go wherever we want (it’s just like setting a custom URL in .htaccess!).
  3. Your short URL to bit.ly’s short URL redirection. This is the bit that redirects everything else from your short domain to bit.ly (e.g. http://sho.rt/BH2mn to http://bit.ly/BH2mn).
So, copy the code below to your .htaccess access file, which should be in the root of your site: e.g. http://sho.rt/.htaccess.
#Lines beginning with # are explanations and can be removed
#Leave the two lines below (setup lines)

	Options +FollowSymLinks
	RewriteEngine on

#Below you can create custom short URLs, like http://sho.rt/MyPhotos
#Anything between "RewriteRule ^" and "$" is the bit after your
	#short domain, e.g. http://sho.rt/THISBIT
#You can take it to a bit.ly code (http://bit.ly/THISBIT)
	#or straight to a website - this won't allow you to track
	#the clicks using bit.ly though.

    RewriteRule ^CUSTOMCODE$ BITLYCODE [L]
    RewriteRule ^CUSTOMCODE$ http://awebsite.com [L]

#The next line redirects your custom domain to your regular domain
	#when no CUSTOMCODE is found
	#e.g. http://sho.rt/ to http://longdomain.com

    RewriteRule ^/?$ http://yourwebsite.com/ [L]

#This bit takes non-custom codes to the equivalent bit.ly code
	#e.g. http://sho.rt/nmk18Y http://bit.ly/nmk18Y

    RewriteRule ^(.*)$ http://bit.ly/$1 [R=301,NC]

Once you’ve copied this code into your .htaccess file, change the bits relevant to your domain (replace THISBIT, http://yourwebsite.com, etc.) and upload it. Now go back to bit.ly and confirm that your A record has been verified (note, mine took a day to do! Normally they take less time).

Once it’s been confirmed, go back to your DNS manager and set the A record to point to its original IP address (your hosting). Now, instead of the A record redirecting all traffic to bit.ly, your .htaccess is managing it so you can have custom URLs and root redirection!

Voila!

Any problems? I’ll be watching the comments so give me a shout there (it’s public so others can see and perhaps solve their problem at the same time).

Enjoy.

  • Pingback: Add Custom Short URLs to your bit.ly .htaccess Redirects… | Blogwin

  • inon

    now, if I want to use bit.ly’s API to set a public domain short service, how do I do that on my root domain?

    • http://jamiegoodwin.net Jamie

      Instead of using DNS forwarding? I haven’t played with bit.ly’s API but I would start here: http://code.google.com/p/bitly-api/

      However, you might want to investigate something like YOURLS: http://yourls.org/, they offer a self-hosted URL shortening service.

  • http://www.edetroit.co Joshua Mango

    Thanks for this. I’m new to DNS A Records, but how would I set it? thanks

    • http://jamiegoodwin.net Jamie

      If you’re going bit.ly’s route of changing your DNS A-records, the trick above won’t work…

      This replaces what bit.ly ask you to do; mimicking the A-record change by using .htaccess. Using A-records won’t allow you to redirect the root of the site, or set custom short URLs.

      If you do want to change your A-records though, you need to go through your registrar or hosting provider (whoever provides the nameserver)…

  • Aleksandrs

    Great stuff, thanks!!

    But can you advise is there a way to code htaccess not to forward some urls?

    Like, if I have a page on http://sho.rt/page which I’d like to display?

    • http://jamiegoodwin.net Jamie

      This isn’t as easy as I thought it would be so it’s taking longer to figure out than I expected, but I’ll let you know when I’ve done so.

  • http://spjain.in DJ

    Hi. Theres an error in this prog. Even those short links that I have not created on spjain.in but which already exist on bit.ly are being displayed. Idealy, such links should display an error message. But, those links are appearing as if they have been created on spjain.in.
    e.g. idealy, a lick such as spjain.in/random should display an error message, but it is displaying the link of bit.ly/random
    Plz advise

    • http://jamiegoodwin.net cjgwin

      Hi DJ, this isn’t an error I’m afraid. This is exactly how it was designed to work. The short URLs that you create are actually created in bit.ly – once you link your personal short domain to bit.ly it will automatically use that domain as a precursor to the bit.ly URLs you create. However, those URLs are all still held in bit.ly.
       
      You can override the URL held in bit.ly by catching a short URL in your .htaccess file, which is what this tutorial is about. However, anything, and I mean ANYTHING that isn’t in your

    • http://jamiegoodwin.net cjgwin

      Hi DJ, this isn’t an error I’m afraid. This is exactly how it was designed to work. The short URLs that you create are actually created in bit.ly – once you link your personal short domain to bit.ly it will automatically use that domain as a precursor to the bit.ly URLs you create. However, those URLs are all still held in bit.ly.
       
      You can override the URL held in bit.ly by catching a short URL in your .htaccess file, which is what this tutorial is about. However, anything, and I mean ANYTHING that isn’t in your .htaccess will redirect to bit.ly/ThatThing. If you want to have a completely personal URL shortener, which will throw an error on invalid URLs, then I suggest looking into something such as http://yourls.org. It’s a full, self-hosted URL shortener.
       
      It takes time to set up and get your head around though.
       
      Jamie

    • http://jamiegoodwin.net/ cjgwin

      Hi DJ, this isn’t an error I’m afraid. This is exactly how it was designed to work. The short URLs that you create are actually created in bit.ly – once you link your personal short domain to bit.ly it will automatically use that domain as a precursor to the bit.ly URLs you create. However, those URLs are all still held in bit.ly.   You can override the URL held in bit.ly by catching a short URL in your .htaccess file, which is what this tutorial is about. However, anything, and I mean ANYTHING that isn’t in your .htaccess will redirect to bit.ly/ThatThing. If you want to have a completely personal URL shortener, which will throw an error on invalid URLs, then I suggest looking into something such as http://yourls.org. It’s a full, self-hosted URL shortener. It takes time to set up and get your head around though.   Jamie

  • http://www.hollbeck.net/ Hendryk

    Thanks a lot for that tutorial – just what I was looking for, as I didn’t want my domain root to point to bitly and be able to have my custom shortlink like http://hch.li/facebook

    • http://jamiegoodwin.net/ cjgwin

       @Hendryk You’re most welcome my friend. I’m glad you found it useful.

      • http://www.hollbeck.net/ Hendryk

         @cjgwin  btw, your example on the blinktag’s page in the comments made it more clear how to add the custom short-urls correctly. maybe add the example in the above post too? ;)

  • francesolimpo

    Hi there, thanks for this post! Quick question: when you say replace THISBIT, what do you replace it with? Or did you mean to replace “http://sho.rt” with your own custom domain?

    • http://www.hollbeck.net/ Hendryk

       @francesolimpo just take the last part of the bit.ly address after the slash, which is “THISBIT” in the above example. If you have bit.ly/M1X8iG you want to redirect to from your sho.rt domain and want it to be sho.rt/somesite place the following code:
      ‘ RewriteRule ^somesite$ M1X8iG[L] ‘
      This will then redirect sho.rt/somesite to the bit.ly address.

    • http://www.hollbeck.net/ Hendryk

      @francesolimpo
      just take the last part of the bit.ly address after the slash, which is “THISBIT” in the above example. If you have http://bit.ly/M1X8iG you want to redirect to from your sho.rt domain and want it to be http://sho.rt/somesite place the following code:
       
      RewriteRule ^somesite$ M1X8iG[L]
       
      This will then redirect http://sho.rt/somesite to the bit.ly address.

  • CharlesHamdoune

    I am trying to combine this solution with my Joomla CMS htaccess file, necessary for the correct SEO redirecting. There is a problem. When I add this code, bit.ly replaces mydomain.com. So instead of getting mydomain.com/something, the user is redirected to the non-existent bit.ly/something
     

  • Pingback: Custom Short URL with Bitly for WordPress Site - Epic Magazine

  • EpicMagazinet

    Thanks a lot Jamie! … After trying YOURLS, wanted to do something new. And I did what I could .. http://emgz.in/UNAXfL ;)

    • http://jamiegoodwin.net/ cjgwin

      Cool – thanks for the post guys. Really like it :-)