def remove_dot_segments(s):
    """Remove dot segments from the string.

    See also Section 5.2.4 of :rfc:`3986`.
    """
    segments = s.split('/')
    output = []
    for segment in segments:
        if segment == '.':
            continue
        elif segment != '..':
            output.append(segment)
        elif output:
            output.pop()
    if s.startswith('/') and (not output or output[0]):
        output.insert(0, '')
    if s.endswith(('/.', '/..')):
        output.append('')
    return '/'.join(output)
