views:

449

answers:

5

My PHP site in maintenance. How to redirect visitors to a "site in maintenance" single page?

I heard something about app_offline.htm for ASP.NET.

Is there something similar for PHP?

  • I want that every page from "mysite.com" be redirected to "maintenance.php";
  • I don't want to do the minimum modification in the existing site pages, ideally no one.

Apache version 2.2.15 under Linux
PHP version 5.2.13

+12  A: 

You really don't give enough details (server, framework, etc). If you are on Apache, add to your .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]
Coronatus
Last time I checked, URL Rewriting doesn't work on Windows - you might want to note that
Charlie Somerville
What do you mean? I can use mod_rewrite on windows just fine.
Ikke
Really? As I said - last time I checked it didn't work at all
Charlie Somerville
I have Apache and PHP see details in edit. I set "maintenance.html" instead of "back-soon-updating.php". Any chanage to the site.
serhio
@serhio: You might also want to change `301` (permanent redirect) to `307` (temporary redirect). Otherwise visitors will have their bookmarks updated to `maintenance.html` and search engines will think the other pages no longer exist, when in reality you are only updating your site's content.
Zach Johnson
Of course mod_rewrite works on Apache for Windows, always has AFAIK. What makes difficulty is creating a `.htaccess` file - Windows Explorer won't let you do that because of the dot prefix. You can circumvent that with 3rd party tools like Filezilla, or change the `AccessFileName` in httpd.conf.
Pekka
.htaccess created without problems under Windows. Transferred to the Linux hosting provider to the public_html folder(visible site root). The Apache server is under Linux. **As result, the site pages still can be accessed - no change.** Used this: ` # prevent infinite redirect loops` `RewriteCond %{REQUEST_URI} !^/maintenance.php$` ` # not on development server` `RewriteCond %{HTTP_HOST} .com$` ` # let admins enter to verify the update has worked ` `RewriteCond %{QUERY_STRING} !c=u RewriteRule .* maintenance.php [L,R=307]`
serhio
A: 

The PHP way : Create an maintainance.php and add this in your 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;
}
PHP_Jedi
should I add this in all of my web pages? after maintenance finished, should I remove this code from all the existing pages?
serhio
A: 

There is no "Global way", either you have to configure your webserver, to go to a specific site, or if you include a page everywhere, add

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

To redirect your users temporary.

Gnutt
should I add this in all of my web pages? after maintenance finished, should I remove this code from all the existing pages?
serhio
yep, another way to ease the "removal" later, is to do a <?php include("header.php");?> and there, do the header("Location: offline.php"); die(); that way, you only have to remove the redirection from your header.php. And you don't risk keeping a redirect on any forgotten page.
Gnutt
A: 

You should set proper header in response as well (HTTP Response Code 503). There are Apache only ways of doing it using mod_rewrite and mod_headers

However for a simple solution without the need of mod_headers, follow the steps below

  • Use your apache conf / .htaccess as mentioned by Coronatus to redirect all users except developers to a PHP maintenance script. Make sure the redirect is indeed temporary (using a 307 redirect code as mentioned by Zach)
  • In your PHP script, set the response header to following at very begining of the script.

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

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

This should get you what you want.

Anshul
.htaccess created under my Windows. Transferred to the Linux hosting provider to the public_html folder(visible site root). ***As result, the site is always visible.*** The Apache server is under Linux. Used this: ` # prevent infinite redirect loops` `RewriteCond %{REQUEST_URI} !^/maintenance.php$` ` # not on development server` `RewriteCond %{HTTP_HOST} .com$` ` # let admins enter to verify the update has worked ` `RewriteCond %{QUERY_STRING} !c=u RewriteRule .* maintenance.php [L,R=307]`
serhio
1. did it work on your local installation?2. is .htaccess enabled on your linux host?
Anshul
+2  A: 

As my Apache in under Linux I do the following:

Created ".htaccess" file under the site root with the folowing content:

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]

Is to remark that I added to filter the .png files, cause I use an image (/images/mainetnance.png) for the maintenance.php, and this image should remain visible, because of global redirect.

serhio