function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Girish ParwaniGirish Parwani 

force.com sites URL rewriter issue with URL parameter getting changed

i recently started on a project which uses Force.com sites and site has implemented URL Rewriter class, i was debugging an issue where URL parameter passed from one site page to another was getting changed, i wanted to know if there can be an issue with Rewriter class implementation :
 
global class SiteRewriterEP implements Site.UrlRewriter {
    //Variables to represent the user-friendly URLs for
    //JOB pages
    String JOB_PAGE = '/careers/';

    //Variables to represent my custom Visualforce pages
    String JOB_VISUALFORCE_PAGE = '/job_details?jid=';

    global PageReference mapRequestUrl(PageReference
            myFriendlyUrl){
        // Generice Method called for       
        PageReference returnPage = SiteRewriterHelper.mapURLRequested(myFriendlyUrl, JOB_PAGE, JOB_VISUALFORCE_PAGE);
        return returnPage;
    }
    global List<PageReference> generateUrlFor(List<PageReference> 
            mySalesforceUrls){
        List<PageReference> returnPages = SiteRewriterHelper.generateURLRequested(mySalesforceUrls, JOB_PAGE, JOB_VISUALFORCE_PAGE);
        return returnPages;
    }
}

global class SiteRewriterHelper {

    global static PageReference mapURLRequested(PageReference currentPagRef, 
    String FRIENDLY_PAGE, String VISUALFORCE_PAGE){

        string url = currentPagRef.getUrl();
        system.debug(' @@ URL Rewriting 1 @@ ' + url);

        ts2__Job__c job = new ts2__Job__c();

        if(url.startsWith(FRIENDLY_PAGE)){
            system.debug('+++ url is '+ url);
            String name = url.substring(FRIENDLY_PAGE.length(),
                    url.length());
            String tempName = name;
            String parameters = '';     
            if(name.contains('&')){
                //integer i = name.indexOf('&');
                tempName = name.subString(0,name.indexOf('&'));
                parameters = name.substring(name.indexOf('&'), name.length());
            }
            if(url.startsWith(FRIENDLY_PAGE) && (url.substring(FRIENDLY_PAGE.length(),url.length())) != null /*&& !url.contains('&')*/){
                job = [SELECT Id, Job_URL_Parameter__c  FROM ts2__Job__c WHERE Job_URL_Parameter__c =: tempName LIMIT 1];
            }
            //Return a page in Visualforce format
            return new PageReference(VISUALFORCE_PAGE + job.id + parameters);
        }
        else{
            return null;
        }        
    }

    global static list<PageReference> generateURLRequested(
    list<PageReference> currentPagReferences, String FRIENDLY_PAGE, 
    String VISUALFORCE_PAGE){
        //A list of pages to return after all the links 
        //have been evaluated
        system.debug(' @@ URL Rewriting 2 @@ ' + currentPagReferences);
        List<PageReference> myFriendlyUrls = new List<PageReference>();
        //a list of all the ids in the urls
        List<id> jobIds = new List<id>();
        String parameters='';
        // loop through all the urls once, finding all the valid ids
        for(PageReference mySalesforceUrl : currentPagReferences){
            //Get the URL of the page            
            String url = mySalesforceUrl.getUrl();
            //If this looks like an page, transform it
            if(url.startsWith(VISUALFORCE_PAGE)){
                    //Extract the ID from the query parameter
                    //and store in a list
                    //for querying later in bulk.

                    String jid='';
                    String tempJID = url.substring(VISUALFORCE_PAGE.length(),
                    url.length());
                    if(tempJID.contains('&')){
                        jid = tempJID.subString(0,tempJID.indexOf('&'));
                        parameters = tempJID.subString(tempJID.indexOf('&'), tempJID.length());
                    }else{
                        jid = tempJID;
                    }                    
                    jobIds.add(jid);
            }
        }

        List <ts2__Job__c> jobs = new List <ts2__Job__c>();
        if(currentPagReferences.size() > 0 && currentPagReferences.get(0).getUrl().startsWith(VISUALFORCE_PAGE) /*&& !currentPagReferences.get(0).getUrl().contains('&') */){
            jobs = [SELECT Name, Job_URL_Parameter__c FROM ts2__Job__c WHERE Id IN :jobIds];
        }
        // make the new urls
        Integer counter = 0;
        // it is important to go through all the urls again, so that the order
        // of the urls in the list is maintained. 
        for(PageReference mySalesforceUrl : currentPagReferences) {
            //Get the URL of the page
            String url = mySalesforceUrl.getUrl();

            if(url.startsWith(VISUALFORCE_PAGE)){
            myFriendlyUrls.add(new PageReference(FRIENDLY_PAGE + jobs.get(counter).Job_URL_Parameter__c + parameters));
            counter++;
            } else {            
            myFriendlyUrls.add(mySalesforceUrl);
            }
        }
        system.debug(' @@ URL Rewriting 3 @@ ' + myFriendlyUrls);
        //Return the full list of pages
        return myFriendlyUrls;
    }
}