looks like i dnd not take into account a case, where we do this for a subpage :)
it should be: 
.replace(/\/$/,"").replace(/\//gi,"_"));

> url = "http://example.com/dir/subdir/another/foo/bar.html";
'http://example.com/dir/subdir/another/foo/bar.html'
> console.log(url.replace("http://","").replace(/\/$/,"").replace(/\//gi,"_"));
example.com_dir_subdir_another_foo_bar.html

and it would work for trailing slash, due to its removal:
> url = "http://example.com/dir/subdir/another/foo/bar/";
'http://example.com/dir/subdir/another/foo/bar/'
> console.log(url.replace("http://","").replace(/\/$/,"").replace(/\//gi,"_"));
example.com_dir_subdir_another_foo_bar