sub abs_url { my ( $relative, $base ) = @_; return $relative if $relative =~ m{ \A http:// }ix; my ( $host, $hostrelative_abs ) = $base =~ m{ \A http:// # skip scheme ([^/]*) # capture hostname /* # skip front slashes (.*?) # capture everything that follows, but [^/]* # leave out the optional final non-directory component \z }ix; $hostrelative_abs = '' if $relative =~ m!^/!; my $abs_url = join '/', $host, $hostrelative_abs, $relative; # replace '//' or '/./' with '/' 1 while $abs_url =~ s{ / \.? (?=/|\z) }{}x; # remove '/foo/..' (but be careful to skip '/../..') 1 while $abs_url =~ s{ / (?!\.\.) [^/]+ / \.\. (?=/|\z) }{}x; return "http://$abs_url"; }