现场维修。如何重定向访问者?

时间:2010-03-27 21:14:53

标签: php apache redirect maintenance

我的PHP网站维护。如何将访问者重定向到“维护站点”单页?

我听说过ASP.NET的app_offline.htm

PHP有类似的内容吗?

  • 我希望“mysite.com”的每一页都被重定向到“maintenance.php”;
  • 我不想在现有网站页面中做最小的修改,理想情况下没有人。
  Linux下的

Apache版本 2.2.15   PHP版本 5.2.13

5 个答案:

答案 0 :(得分:14)

你真的没有提供足够的细节(服务器,框架等)。如果您使用的是Apache,请添加到.htaccess:

# prevent infinite redirect loops
RewriteCond %{REQUEST_URI} !^/back-soon-updating.php$
# not on development server
RewriteCond %{HTTP_HOST} .com$
# let admins enter to verify the update has worked
RewriteCond %{QUERY_STRING} !c=u
RewriteRule .* back-soon-updating.php [L,R=307]

答案 1 :(得分:5)

作为我在Linux下的Apache,我执行以下操作:

在网站根目录下创建了“.htaccess”文件,其中包含以下内容:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{REQUEST_URI} !/images/mainetnance.png$
RewriteCond %{REQUEST_URI} !/maintenance.php$

# here filter the developer's IP/
#RewriteCond %{REMOTE_HOST} !^888\.888\.888\.888

RewriteRule $ /maintenance.php [R=302,L]

要注意我添加过滤.png文件,因为我使用了一个图像(/images/mainetnance.png)作为maintenance.php,并且由于全局重定向,此图像应保持可见。

答案 2 :(得分:2)

PHP方式:创建一个maintainance.php并将其添加到index.php中:

$offline = true; // <== change this to false when you go online again.

if($offline){
  header("Location: maintainance.php");  // <== redirects all to maintainance.php
  exit;
}

答案 3 :(得分:1)

您也应该在响应中设置正确的标头(HTTP响应代码503)。 Apache只有使用mod_rewritemod_headers

的方法

但是,对于不需要mod_headers的简单解决方案,请按照以下步骤操作

  • 使用Coronatus提到的apache conf / .htaccess将除开发人员之外的所有用户重定向到PHP维护脚本。确保重定向确实是临时的(使用Zach提到的307重定向代码)
  • 在PHP脚本中,在脚本开始时将响应标头设置为以下内容。
  

header('HTTP/1.1 503 Service Temporarily Unavailable') //send the proper response code

     

header('Retry-After: 3600') //Retry after an hour

这可以让你得到你想要的东西。

答案 4 :(得分:0)

没有“全球方式”,您必须配置您的网络服务器,转到特定网站,或者如果您在任何地方都包含页面,请添加

header("Location: offline.php");
die();

暂时重定向您的用户。

相关问题