83

I'm considering using the following code during a website launch phase to show users a down for maintenance page while showing me the rest of the site.

Is there a way to show the correct 302 re-direction status to search engines or should I look for another .htaccess based approach?

$visitor = $_SERVER['REMOTE_ADDR'];
if (preg_match("/192.168.0.1/",$visitor)) {
    header('Location: http://www.yoursite.com/thank-you.html');
} else {
    header('Location: http://www.yoursite.com/home-page.html');
};
Paulo Boaventura
  • 1,365
  • 1
  • 9
  • 29
toomanyairmiles
  • 6,465
  • 8
  • 43
  • 71

6 Answers6

154

For a 302 Found, i.e. a temporary redirect do:

header('Location: http://www.example.com/home-page.html');
// OR: header('Location: http://www.example.com/home-page.html', true, 302);
exit;

If you need a permanent redirect, aka: 301 Moved Permanently, do:

header('Location: http://www.example.com/home-page.html', true, 301);
exit;

For more info check the PHP manual for the header function Doc. Also, don't forget to call exit; when using header('Location: ');

But, considering you are doing a temporary maintenance (you don't want that search engines index your page) it's advised to return a 503 Service Unavailable with a custom message (i.e. you don't need any redirect):

<?php
header("HTTP/1.1 503 Service Unavailable");
header("Status: 503 Service Unavailable");
header("Retry-After: 3600");
?><!DOCTYPE html>
<html>
<head>
<title>Temporarily Unavailable</title>
<meta name="robots" content="none" />
</head>
<body>
   Your message here.
</body>
</html>
dynamic
  • 46,985
  • 55
  • 154
  • 231
  • 8
    The syntax is `void header ( string $string [, bool $replace = true [, int $http_response_code ]] )` - http://php.net/manual/en/function.header.php – Vitamin Feb 20 '12 at 15:41
  • 3
    OP asked for redirect during maintenance, in that case he must do temporary redirect with 302, not 301. In case of 301 aka permanent redirect, browser will never try that page but go to redirected one. – seven Feb 11 '14 at 17:50
  • 3
    @michaeld Never, ever test placeholder code as if it were production code. It doesn’t matter what example URL is used in an answer—you should always substitute your own, known URL in its place before running the code. – Janus Bahs Jacquet May 10 '17 at 16:22
  • The point is: should we redirect the client with a 302 and then issue a 503 page, or should we just set up a rewrite rule which would issue a 503 page directly to each client's request? – tonix Aug 15 '18 at 19:00
20

The following code will issue a 301 redirect.

header('Location: http://www.example.com/', true, 301);
exit;
Vitamin
  • 1,526
  • 1
  • 13
  • 27
7

I don't think it really matters how you do it, from PHP or htaccess. Both will accomplish the same thing.

The one thing I want to point out is whether you want the search engines begin to index your site in this "maintenance" phase or not. If not, you could use the status code 503 ("temporarily down"). Here's a htaccess example:

RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !=503
RewriteCond %{REMOTE_HOST} ^192\.168\.0\.1
ErrorDocument 503 /redirect-folder/index.html
RewriteRule !^s/redirect-folder$ /redirect-folder [L,R=503]

In PHP:

header('Location: http://www.yoursite.com/redirect-folder/index.html', true, 503);
exit;

With the current PHP redirect code you're using, the redirect is a 302 (default).

binar
  • 1,197
  • 1
  • 11
  • 24
  • I'm not sure PHP (or the underlying server, in turn) will allow an 503 status code through along with the `Location` header! In fact, I'm almost sure [it wouldn't](http://php.net/manual/en/function.header.php). (I also vaguely recall I had a similar mistake when writing a proxy.) You should most likely use the `header("HTTP/1.0 503 Service not available");` form as also shown in @dynamic's example. – Sz. Oct 10 '18 at 21:43
3

Did you check what header you're getting? Because you should be getting a 302 with above.

From the manual: http://php.net/manual/en/function.header.php

The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>
Nanne
  • 64,065
  • 16
  • 119
  • 163
3

From the PHP documentation:

The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.

so you are already doing the right thing.

Rob Pridham
  • 4,780
  • 1
  • 26
  • 38
1

save this file in the same directory as .htaccess

RewriteEngine on
RewriteBase / 

# To show 404 page 
ErrorDocument 404 /404.html

# Permanent redirect
Redirect 301 /util/old.html /util/new.php

# Temporary redirect
Redirect 302 /util/old.html /util/new.php