So the problem that I am having is I am trying to get the requested URL after that displays a 404 and do some Apex logic on it to route them to the correct place.

Now from reading the documentation I figured I could do something like this.


The problem is that returns the URL of the current 404 Visualforce page.

As an example the user requests:

https://site.force.com/something/path/id/72 which is not a valid path and gets redirected to my custom 404 page. The URL on the top of the browser is still https://site.force.com/something/path/id/72.

Yet when my controller calls ApexPages.currentPage().getUrl(), the page I get back from this is /apex/Custom404Page.

How can the controller get https://site.force.com/something/path/id/72?

Also in Visualforce I notice I can call {!$Site.OriginalUrl} to get back /something/path/id/72. Which is what I want. Can I access this global variable anyway in the controller? Is there a easy way without using JS to have the controller get this?


Well that last part helped me find the solution. I can get {!$Site.OriginalUrl} from Apex.

So I can just call Site.getOriginalUrl(); which returns back the right URL. As the method's documentation says it does the following.

getOriginalUrl() - Returns the original URL for this page if it’s a designated error page for the site; otherwise, returns null.

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.