A more robust solution can be coded using information from Google: https://support.google.com/webmasters/answer/80553?hl=en