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

URL rewriter Class return Unauthorized page

Hi all,


I have created a UrlRewriterClass for my site. It works correctly properly for most of the web site pages.

But I do not understand why on some returns Unauhorized page.


It has already happened to someone?


Thanks in advance for reply!


An unauthorization error can display for two reasons, either there is an error in the URL Rewriter on there is an error in the page being displayed.


You can use standard apex debug statements to narrow down the problem.


If you're still not having any luck finidng the issue, I suggest posting the simplest example possible to reproduce the problem and a member of the community may be able to help you.


Here's my UrlRewriterClass:


global class esa_ClasseURLrewriter implements Site.UrlRewriter {

     // The first global method for mapping external URL to an internal one
     global PageReference mapRequestUrl(PageReference myFriendlyUrl){
        esa_Pagine__c[] p = [select Name, URL_rinominato__c, Contenuto__c from esa_Pagine__c where sito_web__c = 'Camper Usati'];
        PageReference UrlMascherato = myFriendlyUrl;
        String url = myFriendlyUrl.getUrl();
        integer fineFOR = p.size();
      for (integer i=0; i<fineFOR; i++)
        if(fineFOR > 0) {
        PAGINA = p[i].Name; // example: '/esa_contatti'
        SCHEDA = p[i].URL_rinominato__c; // example '/contatti'
         CONTENUTO = p[i].Contenuto__c;
            if(CONTENUTO == 'statico') {
            UrlMascherato = new PageReference(PAGINA);
            return UrlMascherato;
            else if(CONTENUTO == 'dettaglio camper') {
             String name = url.substring(SCHEDA.length(),url.length());
              esa_Camper__c[] site_page = new List<esa_Camper__c>();
              site_page = [select id from esa_Camper__c where URL_completo__c =:name LIMIT 1];
              if( site_page.size() >0 && site_page[0].id != null){ 
              UrlMascherato = new PageReference(PAGINA + site_page[0].id);
              return UrlMascherato;
            else if(CONTENUTO == 'dettaglio marketplace'){
            String nome_macro = url.substring(SCHEDA.length(),url.length());
              esa_Marketplace__c[] macro = new List<esa_Marketplace__c>();
              macro = [select id from esa_Marketplace__c where URL_completo__c =:nome_macro LIMIT 1];
              if( macro.size() > 0 && macro[0].id != null) 
              UrlMascherato = new PageReference(PAGINA + macro[0].id);
              return UrlMascherato; 
            else if(CONTENUTO == 'parametri invariati'){
            String parametri = url.substring(SCHEDA.length(),url.length()); 
          UrlMascherato = new PageReference(PAGINA + parametri);
            return UrlMascherato;
          // fine if "Finefor >0
        // fine ciclo
        //If the URL isn't in the form of a cmasforce page, continue with the request
       return null;
      // The second global method for mapping internal Ids to URLs
      global List<PageReference> generateUrlFor(List<PageReference> mySalesforceUrls){
      esa_Pagine__c[] p = [select Name, URL_rinominato__c, Contenuto__c from esa_Pagine__c where sito_web__c = 'Camper Usati'];
        //A list of pages to return after all the links have been evaluated
        List<PageReference> myFriendlyUrls = new List<PageReference>();
        for(PageReference mySalesforceUrl : mySalesforceUrls){
          //Get the URL of the page
          String url = mySalesforceUrl.getUrl();
          //If this looks like a page that needs to be mapped, transform it
          boolean corrispondenza = false;
          for (integer i=0; i<p.size(); i++)
        PAGINA = p[i].Name;
        SCHEDA = p[i].URL_rinominato__c;
         CONTENUTO = p[i].Contenuto__c;
              if(CONTENUTO == 'statico')
              myFriendlyUrls.add(new PageReference(SCHEDA)); 
                else if (CONTENUTO == 'parametri invariati'){
                String parametri = url.substring(PAGINA.length(),url.length());
                myFriendlyUrls.add(new PageReference(SCHEDA + parametri)); 
                  else if(CONTENUTO == 'dettaglio marketplace'){
                  String id_place = mySalesforceUrl.getParameters().get('place_id');
                  esa_Marketplace__c[] nome_macro = new List<esa_Marketplace__c>();
                  nome_macro = [select URL_completo__c from esa_Marketplace__c where id =:id_place LIMIT 1];
                  if (nome_macro.size() > 0 && nome_macro[0].URL_completo__c != null)
                  myFriendlyUrls.add(new PageReference(SCHEDA + nome_macro[0].URL_completo__c)); 
                  else myFriendlyUrls.add(mySalesforceUrl);
               else if(CONTENUTO == 'dettaglio camper'){
               String id = mySalesforceUrl.getParameters().get('veicolo_id');
               esa_Camper__c[] site_page2 = new List<esa_Camper__c>();
               site_page2 = [select URL_completo__c from esa_Camper__c where id =:id LIMIT 1];
               if (site_page2.size() > 0 && site_page2[0].URL_completo__c != null) {
               myFriendlyUrls.add(new PageReference(SCHEDA + site_page2[0].URL_completo__c));
               else myFriendlyUrls.add(mySalesforceUrl);
               corrispondenza = true; //when the Name of the page is found.
       // fine "FOR" interno
         // if it not found anything, add the original links.. {
            // se non ha trovato la pagina dentro l'oggetto (corrispondenza = false) allora devi aggiungerla lo stesso alla lista
               if (corrispondenza == false) {
      }// fine ciclo lista del mySalesforceURLs
      //Return the full list of pages
      return myFriendlyUrls;


In my class, I use a custom object "esa_Pagine__c" which has the name of all visualforce pages and a custom field which has the name we want after the url rewriting.

Another field contains a picklist which "filter" the visualforce pages , based on the dynamic content in there.


The problem is only when Contenuto__c = 'statico'  (example: page CONTACTS)


Here is my site:



Thanks in advance at all.