public String rewriteUrl(String url, String requestUrl, String baseUrl, String visibleBaseUrl, boolean absolute) {

        // Do not rewrite Urls starting with ESI variables
        // This could be improved by detecting we are in an 'esi:vars' block,
        // but this would link the rewriter with ESI parsing.
        if (url.startsWith("$(")) {
            return url;
        }

        // Base url should end with /
        if (!baseUrl.endsWith("/")) {
            baseUrl = baseUrl + "/";
        }
        URI baseUri = UriUtils.createURI(baseUrl);

        // If no visible url base is defined, use base url as visible base url
        if (!visibleBaseUrl.endsWith("/")) {
            visibleBaseUrl = visibleBaseUrl + "/";
        }
        URI visibleBaseUri = UriUtils.createURI(visibleBaseUrl);

        // Build the absolute Uri of the request sent to the backend
        URI requestUri;
        if (requestUrl.startsWith(visibleBaseUrl)) {
            requestUri = UriUtils.createURI(requestUrl);
        } else {
            requestUri = UriUtils.concatPath(baseUri, requestUrl);
        }

        // Interpret the url relatively to the request url (may be relative)
        URI uri = UriUtils.resolve(url, requestUri);
        // Normalize the path (remove . or .. if possible)
        uri = uri.normalize();

        // Try to relativize url to base url
        URI relativeUri = baseUri.relativize(uri);
        // If the url is unchanged do nothing
        if (relativeUri.equals(uri)) {
            LOG.debug("url kept unchanged: [{}]", url);
            return url;
        }
        // Else rewrite replacing baseUrl by visibleBaseUrl
        URI result = visibleBaseUri.resolve(relativeUri);
        // If mode relative, remove all the scheme://host:port to keep only a url relative to server root (starts with
        // "/")
        if (!absolute) {
            result = UriUtils.removeServer(result);
        }
        LOG.debug("url fixed: [{}] -> [{}]", url, result);
        return result.toString();
    }
public String rewriteUrl ( String url , String requestUrl , String baseUrl , String visibleBaseUrl , boolean absolute ) { if ( url . startsWith ( "$(" ) ) { return url ; } if ( ! baseUrl . endsWith ( "/" ) ) { baseUrl = baseUrl + "/" ; } URI baseUri = UriUtils . createURI ( baseUrl ) ; if ( ! visibleBaseUrl . endsWith ( "/" ) ) { visibleBaseUrl = visibleBaseUrl + "/" ; } URI visibleBaseUri = UriUtils . createURI ( visibleBaseUrl ) ; URI requestUri ; if ( requestUrl . startsWith ( visibleBaseUrl ) ) { requestUri = UriUtils . createURI ( requestUrl ) ; } else { requestUri = UriUtils . concatPath ( baseUri , requestUrl ) ; } URI uri = UriUtils . resolve ( url , requestUri ) ; uri = uri . normalize ( ) ; URI relativeUri = baseUri . relativize ( uri ) ; if ( relativeUri . equals ( uri ) ) { LOG . debug ( "url kept unchanged: [{}]" , url ) ; return url ; } URI result = visibleBaseUri . resolve ( relativeUri ) ; if ( ! absolute ) { result = UriUtils . removeServer ( result ) ; } LOG . debug ( "url fixed: [{}] -> [{}]" , url , result ) ; return result . toString ( ) ; }
Fixes an url according to the chosen mode.
<p>
Note: urls starting with an ESI variable are not rewriten.

@param url
the url to fix (can be anything found in an html page, relative, absolute, empty...)
@param requestUrl
The incoming request URL (could be absolute or relative to visible base url).
@param baseUrl
The base URL selected for this request.
@param visibleBaseUrl
The base URL viewed by the browser.
@param absolute
Should the rewritten urls contain the scheme host and port

@return the fixed url.