code: static public String getRealPath(String servletUrl, String extUrl) {
          Pattern pat = Pattern.compile("^https??://[^/]*");
          // Deal with potential bad extUrl formats
          Matcher m = pat.matcher(extUrl);
          if (m.find()) {
              extUrl = m.group(0);
          }
  
          String retval = pat.matcher(servletUrl).replaceFirst(extUrl);
          return retval;
      }
docs: Simple utility method deal with a request that has the wrong URL when
  behind a proxy.
  
  @param extUrl The url that the external world sees us as responding to.
  This needs to be up to but not including the last slash like and not
  include any path information http://www.sakaiproject.org - although we do
  compensate for extra stuff at the end.
  @return The full path of the request with extUrl in place of whatever the
  request thinks is the current URL.