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.
You 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:
- 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.
- 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!).
- 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).
#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!
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).