def get_hostname(request):
    """ Extract hostname from request.
    The scrip is taken from http://docs.plone.org/develop/plone/serving/http_request_and_response.html#hostname
    and includes virtualhost support.
    """
    if 'HTTP_X_FORWARDED_HOST' in request.environ:
        host = request.environ['HTTP_X_FORWARDED_HOST']
    elif 'HTTP_HOST' in request.environ:
        host = request.environ['HTTP_HOST']
    else:
        return ''
    host = host.split(':')[0].lower()
    return host
