print rel2abs( 'http://foo.com/aa/bb/cc', './../.././bar.htm' ); sub rel2abs { my ($root, $link) = @_; $link =~ s|^\s*/||; # trim link $root .= '/' unless $root =~ m|/$|; # ensure trailing / # move back up tree in response to ../ and ignore ./ while ( $link =~ s|^(\.?\./)|| ) { next if $1 eq './'; # effectively just delete ./ # trim dirs from root only until we can't go any further! $root =~ s|[^/]+/$|| unless $root =~ m|http://[^/]+/$|i; } return $root.$link; }