I am looking for a script that redirects all pages of a website to a page stating that the whole site is closed on Sundays for religious reasons.
It should redirect only on Sunday. On all other days of the week the site should function as normal.
I'd like to do that at the server level in a .htaccess
file, or in a PHP script.
I could imagine something like this:
$day = date('w');
if ($day == 0) { // Sunday...
header("Location: http://www.domain.com/closedonsunday.html");
}
You can use the following rule in .htaccess
:
RewriteEngine on
#--if WDAY ==0--#
RewriteCond %{TIME_WDAY} 0
#--redirect the domain to /closedonsunday.html--#
RewriteRule ^((?!closedonsunday\.html).*)$ /closedonsunday.html [L,R]
The %{TIME_WDAY}
variable represents the day of the week (0-6).
The code above will redirect all requests to /closedonsunday.html
if the condition is met.
You can get numeric representation of the day of the week with date()
and to redirect:
$day = date('N'); //1 (for Monday) through 7 (for Sunday)
if($day == 7){
header("Location: sunday_page.php");
}
It's quite good to do it via PHP by puting this code on very top of your header.
I wouldn't do this with header
since users, depending on their browser settings, might get a cached copy of the page on days other than Monday.
You can use include
to execute script from another file. At the top of each page add the following:
include 'path/to/closedonsunday.php';
And then closedonsunday.php
would have a very simple check like:
if (date('w') == 0) {
/* ... message here ... */
exit;
}
The exit
is the vital part here as as it will stop PHP dead.
You should also be careful with timezones. Ensure that your server clock is set correctly and that your script knows which timezone it should use!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With