Ant*_*ony 6 php .htaccess redirect
我正在寻找一个脚本,将网站的所有页面重定向到一个页面,说明由于宗教原因,整个网站在星期日关闭.
它应该只在周日重定向.在一周的所有其他日子里,网站应该正常运行.
我想在服务器级别在.htaccess文件或PHP脚本中执行此操作.
我可以想象这样的事情:
$day = date('w');
if ($day == 0) { // Sunday...
header("Location: http://www.domain.com/closedonsunday.html");
}
Run Code Online (Sandbox Code Playgroud)
您可以在以下规则中使用以下规则.htaccess:
RewriteEngine on
#--if WDAY ==0--#
RewriteCond %{TIME_WDAY} 0
#--redirect the domain to /closedonsunday.html--#
RewriteRule ^((?!closedonsunday\.html).*)$ /closedonsunday.html [L,R]
Run Code Online (Sandbox Code Playgroud)
该%{TIME_WDAY}变量表示一周(0-6)的日子.
/closedonsunday.html如果满足条件,上面的代码将重定向所有请求.
您可以使用 和 获取一周中某一天的数字表示形式date()以进行重定向:
$day = date('N'); //1 (for Monday) through 7 (for Sunday)
if($day == 7){
header("Location: sunday_page.php");
}
Run Code Online (Sandbox Code Playgroud)
通过 PHP 将此代码放在标头的最顶部是非常好的。