Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

301 or 302 Redirection With PHP

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');
};
like image 234
toomanyairmiles Avatar asked Feb 20 '12 15:02

toomanyairmiles


People also ask

Which is better 301 or 302 redirect?

Each redirect serves a different purpose. For a permanent change that will rank for SEO, a 301 redirect is necessary and understood by search engines. 302 redirect should only be used if it is a temporary change, and they often get used because it's easier to create that instance than the permanent 301 redirect.

How do I make a redirect in PHP?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.

When would you use a 302 redirect?

When Should You Use 302 Redirects? Use this type of redirect if you want to send users to a new site or page for a short period of time, such as when you're redesigning or updating your website. Only use a 302 if you're planning on eventually bringing the old page back or setting up a new one.

What is redirect () in PHP?

PHPServer Side ProgrammingProgramming. The header function in PHP can be used to redirect the user from one page to another. It is an in-built function that sends raw HTTP header to the destination (client).


4 Answers

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>
like image 198
dynamic Avatar answered Oct 13 '22 09:10

dynamic


The following code will issue a 301 redirect.

header('Location: http://www.example.com/', true, 301);
exit;
like image 34
Vitamin Avatar answered Oct 13 '22 09:10

Vitamin


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

like image 7
binar Avatar answered Oct 13 '22 11:10

binar


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;
?>
like image 3
Nanne Avatar answered Oct 13 '22 09:10

Nanne