Hacker News new | ask | show | jobs
by bmelton 5368 days ago
With the rise of cloud services, and their relative inability to serve naked domains, there are a couple of easy fixes for this.

The first is to create a vhost in your webserver that serves an index.htm page on your NON-www url. The contents of that page would simply redirect users to the www.yourdomain url.

If that seems like too much work, an even easier fix (truncated for brevity) is to do something like this:

  var url = window.location.href;
  if(url.indexOf("http://www") != 0) {
    window.location.replace("http://www.yourdomain/");
  }
You'd obviously want to capture the remainder of the querystring and add it in, but that's the general idea.
4 comments

A more efficient and search-engine friendly approach is to perform a 301 redirect. For a single site, in Apache this would look like

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example\.com [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Why not plain old HTTP 301/302 redirect?
Please, please use a redirect. The GP's suggestion is not something you want to do.

Even better, just use http://wwwizer.com/ (look for the free redirect service banner on top).

ooh, this looks really problematic on multiple levels (why wait for JS etc).

A simple solution, which I've used--and I think the OPs situation is similar--is to just have an additional A-record pointed to the ELB, this should work unless the OP is doing something really fancy. No need for route-53, even go-daddy will let you do this.

However, route-53 gives you much better TTL, so if you ever need to re-route..

ELBs change IP if they have to scale up. You really shouldn't point an A record at one.
...which is why they recommend you never use the ip, instead AWS provides a hostname, which is invariant...
Which is why you can't use an A record, as I stated.
The other thing to do is to specify your canonical page so search engines know to use the www subdomain:

http://googlewebmastercentral.blogspot.com/2009/02/specify-y...