0

I have an inactive site like http://abc-company.cs95.force.com/Event I have set Inactive site home page to visualforce page RedirectEvents. When user navigates to a site page which is like http://abc-company.cs95.force.com/Event or http://abc-company.cs95.force.com/Event/Event_ABC, I am getting navigated to visualforce page RedirectEvents. In visualforce page controller i need to find out that from which URL did the user end up on RedirectEvents page. How can this be achieved ?

Visualforce page

<apex:page controller="RedirectEventsController" action="{!redirectForEvents}" showHeader="false" sidebar="false">
    <h1>Redirecting to corresponding event on Avature</h1>
</apex:page>

Controller

public class RedirectEventsController {
public PageReference redirectForEvents() {
String hostVal  = ApexPages.currentPage().getHeaders().get('Host');
String urlVal = Apexpages.currentPage().getUrl();
String fullUrl = hostVal + urlVal;
System.debug('----------- fullUrl value is -------' + fullUrl);
String prevUrl = ApexPages.currentPage().getParameters().get('retURL');
System.debug('----prevUrl---' + prevUrl);
System.debug(ApexPages.currentPage().getParameters());
return null;
}
}
  • While trying this : Site site = [SELECT Subdomain, UrlPathPrefix FROM Site]; String url1 = 'https://' + site.Subdomain + '.secure.force.com/' + site.UrlPathPrefix; System.debug('------ url1-------' + url1); i got this error : System.LimitException: SOQL currently not allowed – Walker May 23 at 14:05
1

In redirects SF uses the retURL paramameter to the new page. To access it in Apex do ApexPages.currentPage().getParameters().get('retURL')

You need to make sure the Salesfroce Referer URL prodection is not disabled. Go to Setup -> Session Settings -> Referrer URL Protection

  • is it because of site guest user i am seeing error "SOQL currently not allowed". How can this be prevented ? – Walker May 23 at 14:10
  • 1
    to use the SITE object in a query, the user needs the “View Setup and Configuration” permission. @Walker – PartOfTheOhana May 23 at 14:19
1

I use the following code to determine what the URL is of the page that is currently being displayed to the user.

In the APEX Controller Class, create a public variable:

public String theURL {get;set;}
public PageReference DoUpdatePage() {return null;}

in the VisualForce Page, add this code:

<apex:form styleClass="hidden" id="theURLForm">
    <!-- Captures the intial URL used to access the site -->
    <apex:inputHidden value="{!theURL}" id="OriginURL" />
    <apex:actionFunction name="DoInitUrl" action="{!DoUpdatePage}" reRender="theURLForm" />
    <script>
        fieldOriginURL = document.getElementById('{!$Component.theURLForm.OriginURL}');
        if ( fieldOriginURL.value == '' ) {
            fieldOriginURL.value = document.location.href; 
            if ( fieldOriginURL.value == '' ) fieldOriginURL.value = 'unknown'; // prevent loop if "document.location.href" is blank for some reason
            DoInitUrl(); // updates the variable in the APEX class
        }
    </script>
</apex:form>

It will only set the value of "theURL" in the APEX class once, so as long as the session is active, theURL will contain the first URL even if the page redirects to another URL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.