Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I "close" a website on Sundays only

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");
}
like image 349
Anthony Avatar asked Mar 08 '16 10:03

Anthony


3 Answers

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.

like image 195
Amit Verma Avatar answered Sep 29 '22 06:09

Amit Verma


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.

like image 32
mitkosoft Avatar answered Sep 29 '22 06:09

mitkosoft


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!

like image 33
timclutton Avatar answered Sep 29 '22 07:09

timclutton