code: public static String rehostLocation (
          HttpServletRequest req, String servername)
      {
          StringBuffer buf = req.getRequestURL();
          String csname = req.getServerName();
          int csidx = buf.indexOf(csname);
          if (csidx != -1) {
              buf.delete(csidx, csidx + csname.length());
              buf.insert(csidx, servername);
          }
          String query = req.getQueryString();
          if (!StringUtil.isBlank(query)) {
              buf.append("?").append(query);
          }
          return buf.toString();
      }
docs: Recreates the URL used to make the supplied request, replacing the
  server part of the URL with the supplied server name.