我怎样才能在周日“关闭”网站

时间:2023-01-17 10:46:28

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.

我想在服务器级别的.htaccess文件或PHP脚本中执行此操作。

I could imagine something like this:

我可以想象这样的事情:

$day = date('w');
if ($day == 0) { // Sunday...
    header("Location: http://www.domain.com/closedonsunday.html");
}

4 个解决方案

#1


6  

You can use the following rule in .htaccess:

您可以在.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).

%{TIME_WDAY}变量表示星期几(0-6)。

The code above will redirect all requests to /closedonsunday.html if the condition is met.

如果满足条件,上面的代码会将所有请求重定向到/closedonsunday.html。

#2


1  

You can get numeric representation of the day of the week with date() and to redirect:

您可以使用date()获取星期几的数字表示并重定向:

$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.

通过将此代码放在标题的顶部,通过PHP完成它是相当不错的。

#3


1  

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从另一个文件执行脚本。在每个页面的顶部添加以下内容:

include 'path/to/closedonsunday.php';

And then closedonsunday.php would have a very simple check like:

然后closedonsunday.php将有一个非常简单的检查,如:

if (date('w') == 0) {
    /* ... message here ... */
    exit;
}

The exit is the vital part here as as it will stop PHP dead.

退出是这里至关重要的部分,因为它会阻止PHP死机。

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!

你也应该小心时区。确保您的服务器时钟设置正确,并且您的脚本知道它应该使用哪个时区!

#4


1  

In my opinion, You should use .htaccess file. Date and time values in .htaccess come in the form

在我看来,你应该使用.htaccess文件。 .htaccess中的日期和时间值以表格形式出现

%{TIME_XXXX}

%{} TIME_XXXX

where XXXX is the type of date or time you want.

其中XXXX是您想要的日期或时间类型。

In case if you want to redirect a generic url to one which contains today’s date, you might use:

如果您想将通用网址重定向到包含今天日期的网址,您可以使用:

RewriteRule ^posts/today$ /posts/%{TIME_YEAR}-%{TIME_MON}-%{TIME_DAY}

RewriteRule ^发布/今天$ / posts /%{TIME_YEAR} - %{TIME_MON} - %{TIME_DAY}

That would result in /posts/today being redirected to something like /posts/2015-08-27

这将导致/ posts / today被重定向到/ posts / 2015-08-27

If you wanted redirect a page after a date (and time) is password you could use something like the following, where if the date is passed 9am on 27th August 2015 the redirect will happen. We use a simple number comparison of turning the date into an integer and then comparing it.

如果您希望在日期(和时间)为密码后重定向页面,则可以使用以下内容,如果日期在2015年8月27日上午9点过去,则会发生重定向。我们使用简单的数字比较将日期转换为整数然后进行比较。

RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY}%{TIME_HOUR} >2015082709
RewriteRule ^$ /destination/url.html [R=301,L]

In your case

在你的情况下

RewriteCond %{TIME_WDAY} = 0 // condition
RewriteRule URL on which you wants to redirect

For more tutorial you can use the link Tutorial and link 2

有关更多教程,您可以使用链接教程和链接2

If you wants to use PHP way then use the switch. If later you wants to redirect on any other day too then you can manage that here.

如果你想使用PHP方式,那么使用开关。如果以后你想在任何其他日子重定向,那么你可以在这里管理。

$day = date('N'); //1 for Monday , 2 for Tuesday ... 7 For Sunday
//N is  The ISO-8601 numeric representation of a day
switch($day){
case 1:
  header('location: site for Monday');break;
case 2:
  header('location: site link for Tuesday'); break;
}
 . 
 .
case 7: 
 header("Location: http://www.domain.com/closedonsunday.html"); break;
default:
    code to be executed if day is different from all labels; // it is not possible in this case :)

}

}

#1


6  

You can use the following rule in .htaccess:

您可以在.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).

%{TIME_WDAY}变量表示星期几(0-6)。

The code above will redirect all requests to /closedonsunday.html if the condition is met.

如果满足条件,上面的代码会将所有请求重定向到/closedonsunday.html。

#2


1  

You can get numeric representation of the day of the week with date() and to redirect:

您可以使用date()获取星期几的数字表示并重定向:

$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.

通过将此代码放在标题的顶部,通过PHP完成它是相当不错的。

#3


1  

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从另一个文件执行脚本。在每个页面的顶部添加以下内容:

include 'path/to/closedonsunday.php';

And then closedonsunday.php would have a very simple check like:

然后closedonsunday.php将有一个非常简单的检查,如:

if (date('w') == 0) {
    /* ... message here ... */
    exit;
}

The exit is the vital part here as as it will stop PHP dead.

退出是这里至关重要的部分,因为它会阻止PHP死机。

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!

你也应该小心时区。确保您的服务器时钟设置正确,并且您的脚本知道它应该使用哪个时区!

#4


1  

In my opinion, You should use .htaccess file. Date and time values in .htaccess come in the form

在我看来,你应该使用.htaccess文件。 .htaccess中的日期和时间值以表格形式出现

%{TIME_XXXX}

%{} TIME_XXXX

where XXXX is the type of date or time you want.

其中XXXX是您想要的日期或时间类型。

In case if you want to redirect a generic url to one which contains today’s date, you might use:

如果您想将通用网址重定向到包含今天日期的网址,您可以使用:

RewriteRule ^posts/today$ /posts/%{TIME_YEAR}-%{TIME_MON}-%{TIME_DAY}

RewriteRule ^发布/今天$ / posts /%{TIME_YEAR} - %{TIME_MON} - %{TIME_DAY}

That would result in /posts/today being redirected to something like /posts/2015-08-27

这将导致/ posts / today被重定向到/ posts / 2015-08-27

If you wanted redirect a page after a date (and time) is password you could use something like the following, where if the date is passed 9am on 27th August 2015 the redirect will happen. We use a simple number comparison of turning the date into an integer and then comparing it.

如果您希望在日期(和时间)为密码后重定向页面,则可以使用以下内容,如果日期在2015年8月27日上午9点过去,则会发生重定向。我们使用简单的数字比较将日期转换为整数然后进行比较。

RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY}%{TIME_HOUR} >2015082709
RewriteRule ^$ /destination/url.html [R=301,L]

In your case

在你的情况下

RewriteCond %{TIME_WDAY} = 0 // condition
RewriteRule URL on which you wants to redirect

For more tutorial you can use the link Tutorial and link 2

有关更多教程,您可以使用链接教程和链接2

If you wants to use PHP way then use the switch. If later you wants to redirect on any other day too then you can manage that here.

如果你想使用PHP方式,那么使用开关。如果以后你想在任何其他日子重定向,那么你可以在这里管理。

$day = date('N'); //1 for Monday , 2 for Tuesday ... 7 For Sunday
//N is  The ISO-8601 numeric representation of a day
switch($day){
case 1:
  header('location: site for Monday');break;
case 2:
  header('location: site link for Tuesday'); break;
}
 . 
 .
case 7: 
 header("Location: http://www.domain.com/closedonsunday.html"); break;
default:
    code to be executed if day is different from all labels; // it is not possible in this case :)

}

}