Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

RESOLVED: Redirect after short delay on Error 404 page


Go to End


4 Posts   4908 Views

Avatar
DJMatus23

Community Member, 11 Posts

4 May 2009 at 4:40pm

Edited: 08/05/2009 1:56pm

Hi,

Another question regarding 404 pages - I want my 404 page to automatically redirect the user to the front page of the site after 10 seconds. Is it possible to do this via the CMS, or do I need to hack some JavaScript in to the file after publishing?

Cheers
Ronan

P.S. For another 404 question, see: http://silverstripe.org/customising-the-cms/show/259762

Avatar
rbquirke

Community Member, 70 Posts

4 May 2009 at 9:51pm

Another Ronan on the forum? Damn, I was sure I would be unique for a while longer!

Ok, so what you want to do is create an ErrorPage type in your theme:

1) in the \themes\YOURTHEME\templates folder, you should see at least a Page.ss - copy this and rename the copy as ErrorPage.ss
2) in the \themes\YOURTHEME\templates\Layout folder - there will be another Page.ss - again make a copy as an ErrorPage.ss

The Page.ss in the ./templates directory is the main holder file. This will contain the <head> tags to which you can add in a bit of Jscript to redirect to the home page after 10 seconds or whatever you need.

The downside to this is that you will now have two main files in the ./templates directory to maintain. Most page types can work off the standard Page.ss one, only requiring a files in the Layout folder, but aside from that, should work fine.

Don't forget to append ?flush=1 to the querystring to see the changes take effect.

Ronan

Avatar
DJMatus23

Community Member, 11 Posts

5 May 2009 at 12:24pm

Hey Ronan,

Cheers for the help, I take it Silverstripe just looks for the ErrorPage.ss file, and if it doesn't find it there, it just uses the Page.ss file? I've had a quick look at the Page.ss file and I know this is a bit cheeky, but as I only want the redirect on a 404 error (for a 500 I want to leave the user sitting there), can I driectly code a bit of PHP (or Silverstripe code) to check the ErrorCode and thus only include the redirect header for 404s? If so, I'll probably just go with a HTML redirect rather than a Javascript one.

Cheers
Ronan 2.0

P.S. Good name, btw, I love the fact that it's exotic in NZ, I know far too many Ronans at home - kinda like the Irish equivalent of Dave in NZ...

Avatar
DJMatus23

Community Member, 11 Posts

8 May 2009 at 1:55pm

See my other post http://silverstripe.org/customising-the-cms/show/259762 regarding the issue of emailing the sysadmin when a site error occurs. The following code can be added in after the mail command so that the user is redirected to the front page of the site after 5 seconds.

// Once Email is sent, set up the redirect
  $redirectDest = "/";  // i.e. homepage of site
  $redirectDelay = "5000";  // 5000ms (5 sec) delay
  $redirect = '
setTimeout(\'window.location = "'.$redirectDest.'"\','.$redirectDelay.')
';
  if ($_GET["err_code"]=="404") echo $redirect;  // Only do the redirect on 404 errors.

Hope this helps others.

:)
Ronan 2.0