Redirect all requests

Hi,

The website I am working on is for a competition. When it closes, I would like to redirect all requests to a “closed” page. I have set up the page at /closed, what is the best way of ensuring all visitors end up there, no matter the page / url they are trying to view? I tried a route using (:all) but it doesn’t seem to be working

All would be right, but would also include closed, so you need an if statement inside the route that returns the closed page while all other pages are redirected.

What does that mean?

Hi,
Thanks for the quick reply.
The site has several routes, so I just popped this one at the top:

[
  'pattern' => '(:all)',
  'action'  => function() {
    return go('/closed');
  }
],

But this doesn’t seem to be having any effect

This route will lead to an endless redirect when the closed page is called. And it should be

return go('closed')

without the leading slash.

As I wrote, you need a condition for the closed path.

But the problem is that if you have other routes, they will not work with this catch-all. So you need another condition when this route should take effect, e.g. after a given date.

@mikeharrison Feel free to shop around in this plugin’s code which sounds like pretty close to your use case?

Thanks both for your help.

I ended up editing my header.php snippet to add the following:

<?php 
  $today = date("Y-m-d H:i:s");
  $date = "2021-12-20 16:00:00";
  if ($date < $today) {
    if($page->template() != 'closed'): 
      go('closed');
    endif; 
  }
?>

Which seems to be doing the job - thanks Sonja for the pointer about the inifinite redirect