Is the url parsing ok? Is it just the adding final slash the bit that's bad? If so then change your pattern to '#/$#'
But you could just check with substring like, ummm
if (substr($web_url, -1, 1) != '/') $web_url .= '/';
I'm just guessing at the parameter order to substr, but think it's probably alright