I recently helped a client setup a custom domain for their bitly account.  Bitly of course makes this extremely easy except for one small thing:  We wanted the root domain to redirect to their main site.

To give an example, let’s say I had a custom short domain of “amit.st” that I wanted to use for redirecting people to “amitstreet.com”.  The standard approach is to point the A-Record of that domain to Bitly’s IP address.  And for any shared link, this will redirect fine, but if someone gets curious and wants to type in just “http://amit.st” they’ll get redirected over to “http://bit.ly” which is not what I want.

So after a quick Google search, I found the answer, and I’ll share it here:

First: Point your DNS at your own server (i.e. do NOT point your A-Record at Bitly’s IP Address)

Next: use this code base in your .htaccess file:

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^$ xUniVT [L]
RewriteRule ^custom$ yX3Lbm [L]

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

To use this code

1) replace “amit.st” with your own custom domain

2) In your bitly account, create a redirect to your home page

3) In Line 4 of the .htaccess code, replace the “xUniVT” with whatever bitly generates for the hompepage redirect you made in step 2

4) Line 5 is a little bonus: Instead of giving people “amit.st/4rReses” (which is bitly’s autogenerated URLification), you can make your own human-understandable shortness (i.e. amit.st/cool) and replace the “custom” with “cool” (remember, see line 5), and replace the “yX3Lbm” with whatever bitly autogenerates for you.