Removing content- from link | Community
Skip to main content
New Participant
June 1, 2016
Solved

Removing content- from link

  • June 1, 2016
  • 7 replies
  • 6007 views

Hi,

There is requirement to remove the content from page link. Please suggest way forward for the same. Have already done 301 re-direction to remove the content from URL.

But need to remove the content from page link.

 

Regards,

 

Deepak

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

Hvae you tried writing a link rewritter using the org.apache.sling.rewriter.Transformer API? 

7 replies

New Participant
June 6, 2016

Deepaks007 wrote...

Hi,

There is requirement to remove the content from page link. Please suggest way forward for the same. Have already done 301 re-direction to remove the content from URL.

But need to remove the content from page link.

 

Regards,

 

Deepak

 

 Here is complete code for removing the /content from the html source. This is very common use case to remove the /content from page source.

import java.io.IOException;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.rewriter.ProcessingComponentConfiguration;
import org.apache.sling.rewriter.ProcessingContext;
import org.apache.sling.rewriter.Transformer;
import org.apache.sling.rewriter.TransformerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
@Component(metatype = true, label = "Custom Link Transformer", description = "Removes /content/home under specified paths")
@Service(value = TransformerFactory.class)

public class CustomLinkChecker implements Transformer, TransformerFactory{
    
      
 @Property(value = "append-version", propertyPrivate = true)
 private static final String PIPELINE_TYPE = "pipeline.type";
 
 @Property(value = "global", propertyPrivate = true)
 private static final String PIPELINE_MODE = "pipeline.mode";
     private ContentHandler contentHandler;
    Logger log = LoggerFactory.getLogger(CustomLinkChecker.class);

    public CustomLinkChecker()
    {
        log.info("customlinkchecker");
    }
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        contentHandler.characters(ch, start, length);
        
    }
    public void endDocument() throws SAXException {
        contentHandler.endDocument();
        
    }
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        contentHandler.endElement(uri, localName, qName);
        
    }
    public void endPrefixMapping(String prefix) throws SAXException {
        contentHandler.endPrefixMapping(prefix);
        
    }
    public void ignorableWhitespace(char[] ch, int start, int length)
            throws SAXException {
        contentHandler.ignorableWhitespace(ch, start, length);
        
    }
    public void processingInstruction(String target, String data)
            throws SAXException {
    contentHandler.processingInstruction(target, data);
        
    }
    public void setDocumentLocator(Locator locator) {
        contentHandler.setDocumentLocator(locator);
        
    }
    public void skippedEntity(String name) throws SAXException {
        contentHandler.skippedEntity(name);
        
    }
    public void startDocument() throws SAXException {
        log.info("starting of document");
        contentHandler.startDocument();
        
    }
    public void startElement(String uri, String localName, String qName,
            Attributes atts) throws SAXException {
        log.info("starting of element");
        final AttributesImpl attributes = new AttributesImpl(atts);
        log.info("Processing element: " + attributes.getValue("href"));
        final String href = attributes.getValue("href");
        if (href != null && href.startsWith("/content")) {
            log.info("href--" + href);
            for (int i = 0; i < attributes.getLength(); i++) {
                log.info("attribute" + attributes.getQName(i));
                if ("href".equalsIgnoreCase(attributes.getQName(i))) {
                    log.info("hrefvalue" + attributes.getValue("href"));
                    attributes.setValue(i, replaceHref(attributes.getValue("href")));
                }
            }
            
        }
        contentHandler.startElement(uri, localName, qName, attributes);
      
        
    }
    public void startPrefixMapping(String prefix, String uri)
            throws SAXException {
        contentHandler.startPrefixMapping(prefix, uri);
        
    }
    public Transformer createTransformer() {
        // TODO Auto-generated method stub
        return new CustomLinkChecker();
    }
    public void dispose() {
        // TODO Auto-generated method stub
        
    }
    public void init(ProcessingContext arg0,
            ProcessingComponentConfiguration arg1) throws IOException {
        // TODO Auto-generated method stub
        
    }
    public void setContentHandler(ContentHandler arg0) {
        this.contentHandler = arg0;
        
    }
    
   public String replaceHref(String href)
   {
       String newHref = "";
       if(href != null)
       {
        newHref= href.replaceAll("/content"," ");
       }
       return newHref;
   }
    
}

kautuk_sahni
Employee
June 2, 2016

Hi

Adding to what Tomar suggested:-

Please have a look at these reference links:-

Link 1:- http://blogs.adobe.com/contentmanagement/2013/08/20/how-to-change-aems-redirection-http-status-code/

//

Redirection of request URLs in AEM can be configured at the Sling level, typically in the mapping configuration under /etc/map node. Depending on the requirements, there are times when customers would like to change the default HTTP status code for redirection. And here you will find two ways of changing the status code:

The sling:status way

Documented in http://sling.apache.org/documentation/the-sling-engine/mappings-for-resource-resolution.html already, you may use sling:status property to set a specific status code for a particular redirection. To extend the example given on the page linked above:

/etc/map
      +-- http
           +-- example.com.80
           |    +-- sling:redirect = "http://www.example.com/"
           |    +-- sling:status = "301"

Via OSGi configuration

Another way, which really sets the default for all redirection, is to configure the status code at the OSGi bundle level. In the OSGi console, browse the the Configuration page and locate:

Apache Sling Resource Resolver Factory

and change the redirect status field to whatever status code is appropriate (highlighted in diagram below).

[img]http://blogs.adobe.com/contentmanagement/files/2013/08/sling_redirection_status_code.png[/img]

 

Link 2:- http://aem-cq-tutorials.blogspot.in/2014/06/mapping-of-request-urls-in-cqaem.html

//

Problem 1:

Say we need to convert the URL like: www.example.com --> converts to --> /content/mysite/en/

Sol 1:- <map>
           <http jcr:primaryType="sling:OrderedFolder">
                <www.example.com 
                    jcr:primaryType="sling:Mapping"
                    sling:internalRedirect="/content/mysite/en"/>
            </http>
        </map>

 

Validation :- Once we save these changes, we can check to see if everything is working as expected using the Sling Resource Resolver page in the System Console (ie. http://localhost:4502/system/console/jcrresolver).

            

I hope this will help you.

Thanks and Regards

Kautuk Sahni      

Kautuk Sahni
Kunal_Gaba_
New Participant
June 1, 2016

You need to rewrite the links on your page using Sling rewriter. You can refer the following example which is similar to your use case - http://www.cqblueprints.com/tipsandtricks/serving-static-assets-alt-url.html

smacdonald2008
smacdonald2008Accepted solution
New Participant
June 1, 2016

Hvae you tried writing a link rewritter using the org.apache.sling.rewriter.Transformer API? 

New Participant
June 1, 2016

Yes this is required for the publish instance. have done 301 re-redirection using apche re-write rules. When i am browsing the page to like the particular page then content will be part of link.. How to get rid of this?

Jitendra_S_Toma
New Participant
June 1, 2016

I hope this requirement is for publish environment where you want to remove /content directory. Have you explored /etc/map or resource resolver factory configuration

Regards,

Jitendra

smacdonald2008
New Participant
June 1, 2016