简体   繁体   中英

Converting anchor tag with relative URL to absolute URL in HTML content using Java

The situation:

On server A we want to display content from server B in line on server A.

The problem:

Some of the hyperlinks in the content on server B are relative to server B which makes them invalid when displayed on server A.

Given a block of HTML code that contains anchor tags like the following

<a href="/something/somwhere.html">Somewhere</a>

what would be the most efficient way to convert them to

<a href="http://server-b.com/something/somewhere.html">Somewhere</a>

There can be multiple anchor tags in the content, the one catch is that some might be absolute and I want to leave those as they are, I only want to prepend the domain of server B to the relative URLs

Depending on a lot of things around how your web app is set up, and also on your definition of efficient, this might not be what you need or are looking for. But anyway, if you have your HTML as a String (in some late stage of a Filter for example), you could do something like this:

html = html.replaceAll("href=\"/", "href=\"http://server-b.com/")

There is my method, whitch i use for convert relative URLs to absolute. I use it for converting some pages to email body.

public String replaceLinks(String address, String content) throws URISyntaxException{
    //absolute URI used for change all relative links
    URI addressUri = new URI(address);
    //finds all link atributes (href, src, etc.)
    Pattern pattern = Pattern.compile("(href|src|action|background)=\"[^\"]*\"", Pattern.CASE_INSENSITIVE);
    Matcher m = pattern.matcher(content);
    //determines if the link is allready absolute
    Pattern absoluteLinkPattern = Pattern.compile("[a-z]+://.+");
    //buffer for result saving
    StringBuffer buffer = new StringBuffer();
    //position from where should next interation take content to append to buffer
    int lastEnd = 0;
    while(m.find()){
        //position of link in quotes
        int startPos = content.indexOf('"',m.start())+1;
        int endPos = m.end()-1;
        String link = content.substring(startPos,endPos);
        Matcher absoluteMatcher = absoluteLinkPattern.matcher(link);
        //is the link relative?
        if(!absoluteMatcher.find())
        {
            //create relative URL
            URI tmpUri = addressUri.resolve(link);
            //append the string between links
            buffer.append(content.substring(lastEnd,startPos-1));
            //append new link
            buffer.append(tmpUri.toString());
            lastEnd =endPos+1;
        }
    }
    //append the end of file
    buffer.append(content.substring(lastEnd));
    return buffer.toString();
}

hope it helps.

I wouldn't do this in Java; I like to handle view-specific logic in the view layer. I'm assuming this block of code is coming from an AJAX call. So what you can do is get the HTML from the AJAX call and then do this:

jQuery(html).find("a[href]").each(function(index, value) {
  var $a = jQuery(value);
  var href = $a.attr("href");

  if(!/^http:/.test(href)) {
     $a.attr("href", "http://server-b.com" + href);
   }
});

Or if you really want to do this in Java, Lauri's answer will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM