Short Links at base (2nd level) domain name

71 views
Skip to first unread message

Bob Parker

unread,
Jun 29, 2011, 1:15:35 PM6/29/11
to google-apps-la...@googlegroups.com
One of the frustrations I had with Google Apps Short Links originally was the fact that you MUST have a sub (3rd level) domain in order for the service to work. Since these are SHORT links, that sort of defeats the purpose in a way, especially if you got lucky and got a really short domain name :) For example, I got wmd.me, but to use it I would have to add another level, to make it www.wmd.me, or whatever.

I figured out how to get around this using Apache and mod_rewrite. Here's how I did it, if this is of any help to someone else. It can also be used to serve actual files directly from the short link domain, like images, without having to redirect through the actual short link first. 

To use this, you should have mod_rewrite installed and have access to your web server's config files. Familiarity with apache configs would be helpful. 

Legal disclaimer: I'm providing this as helpful information to members of the Google Apps Short Links group in the hope that it may be useful to you, but in no way guarantee that it will work for you "out of the box" or accept responsibility for disruption of service caused by any attempt to implement these modifications.

* My short links are setup in the Google Apps control panel as www.wx.yz, because you MUST have a 3rd level domain for it to work :(

In Apache, you can do the setup two ways:

* Setup a VirtualHost specifically for the domain by itself (consult the apache docs - won't go into that here), or
* Use an existing VirtualHost and just add the base domain as a ServerAlias line.  My short link domain is a service related to a primary website, so I chose this route and added:

    ServerAlias wmd.me

  to the primary website's VirtualHost section. I'm going to talk about this method here, but I've added a full Apache VirtualHost for the first method at the bottom of this email for reference purposes.

* Setup the Rewrite Rules: While the rewrite rules CAN be done in an .htaccess file, if allowed by server configuration and access rules, it is strongly recommended that you NOT do so. This is because the server must read and parse the .htaccess file for *every request*. That's a lot of overhead, especially for a busy server that processes a lot of requests, which is in turn going to slow down processing & your server. If you do this in the Apache config, it will compile and store the rules once, making it many, many times faster.

Within the <VirtualHost doma.in:80>.... </VirtualHost> section for your website, add the following (our fake domain here for example purposes is "doma.in"):

    RewriteEngine On

    RewriteCond %{HTTP_HOST} doma.in
    RewriteRule ^(.*) http://www.doma.in$1 [R=301,L]

If your short links are subject to being deleted or changed, you can change the end of that RewriteRule to be [R,L] since the default redirect type is 302 "Temporary Redirect", or you can explicitly change it to be [R=302,L]. Since I don't delete short links once they are setup and tend to define them rather than use hashed links, I like using 301 "Permanent Redirect". This is important, because when a browser sees that link again in the future, it will see this info in the cache and go directly to the ultimate destination, rather than re-querying the server again - if you send the 301 response. With a 302, it will go back to the server and ask again.

Technically, if you setup the VirtualHost just for the domain "doma.in" by itsel, the RewriteCond would not be required, since all requests coming in would be for that domain, so you can leave that RewriteCond line out. This could speed things up for you.

After that, save the file and check your configuration. If running Apache 1.x run "apachectl configtest", Apache 2.x "apache2ctl configtest" - you should get no warnings or errors. if all is good, restart the server with the same command but "restart" instead of "configtest" and you should be ready to go! Now you can use a TRUE short link, without the 3rd level domain. 

Hope this is helpful to someone!

Bob Parker

Full virtual server for apache for reference:

<VirtualHost doma.in:80>
    ServerAdmin sup...@doma.in
    ServerName doma.in
    DocumentRoot /home/web/domain

    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /home/web/domain/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    RewriteEngine On

    # If the requested file actually exists in /home/web/domain/
    # go ahead and pass the request through and serve it
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule .* - [L]

    # Otherwise, forward the request on to Short Link server
    RewriteRule ^(.*) http://www.doma.in$1 [R=301,L]
</VirtualHost>
Reply all
Reply to author
Forward
0 new messages