Page 1 of 1

How do you enter a 301 redirect in PHP?

Posted: Mon Jun 09, 2014 3:23 am
by simonmlewis
We have many 'Soft 404s' in our Google Webmasters.
I'd like to take the Google Bot, as well as users, to a 301 error, that takes them to the nearest page that is correct, such as the previous page.

I know how to query if the category or product you are visiting doesn't exist. Easy of course. But how do you put into PHP, categ.inc or product.inc, a 301, to take them elsewhere?

Re: How do you enter a 301 redirect in PHP?

Posted: Mon Jun 09, 2014 5:58 am
by Celauran

Re: How do you enter a 301 redirect in PHP?

Posted: Mon Jun 09, 2014 6:30 am
by simonmlewis
I guess this cannot go into an include file, but must be in the index.php template, because it is a HEADER ??

Re: How do you enter a 301 redirect in PHP?

Posted: Mon Jun 09, 2014 8:15 am
by Celauran
That does not follow. The only condition is that headers have not already been sent.

Re: How do you enter a 301 redirect in PHP?

Posted: Tue Jun 10, 2014 4:34 am
by simonmlewis
The problem is - I think - this is in an include file, which has data at the top first.

This script comes in a query where it checks first if the ID is correct. If not, I want it to 301. Otherwise, show the page.

[text]Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\phpMyAdmin\site\index.php:552) in C:\xampp\phpMyAdmin\site\includes\subcateg.inc on line 73[/text]

So whether I put this at the very top of subcateg.inc or not, seems not to matter, because index.php doesn't like it.

I need to get this dynamic, or I will have 700 soft errors to 301 in HTACCESS!!

Re: How do you enter a 301 redirect in PHP?

Posted: Tue Jun 10, 2014 6:59 am
by Celauran
Sounds like the problem is that index.php has already sent output to the browser before 'include categ.inc' is being called, not the fact that it's an included file in and of itself. This may sound like semantics, but it's important to understand the difference and why things are behaving the way they are.

That said, if the include is being called after output has been sent to the browser, it's time to re-examine your work flow. Maybe output buffering will help? Not sure what your code looks like.

Re: How do you enter a 301 redirect in PHP?

Posted: Tue Jun 10, 2014 7:05 am
by simonmlewis
I haev output in the template's menu, and then an include file for the individual pages.
I am thinking of putting this HEADER query into the template itself. I guess the best place would be immediately after:

Code: Select all

<?php
session_start(); 
.... ??
Then query for the category, if it is queried. Subcategory if queried. And Product.....

Re: How do you enter a 301 redirect in PHP?

Posted: Tue Jun 10, 2014 7:23 am
by Celauran
Wouldn't you at least need one query first to determine whether or not to redirect?

As much as possible, try to keep your business logic out of your presentation to avoid situations like this altogether.

Re: How do you enter a 301 redirect in PHP?

Posted: Tue Jun 10, 2014 7:31 am
by simonmlewis

Code: Select all

if(isset($s))
{
  $result = mysql_query ("SELECT DISTINCT subid FROM products WHERE subid = '$s' AND pause = 'off'");
  $num_result = mysql_num_rows($result);
  if ($num_result == "0")
  {
    $resultc = mysql_query ("SELECT catid, catname FROM products WHERE catid = '$c' AND pause = 'off' GROUP BY catid");
    $num_resultc = mysql_num_rows($resultc);
    if ($num_resultc != "0")
    {
      while ($rowc = mysql_fetch_object($resultc))
      {
      $categ = "$rowc->catname"; 
      $findcateg ="/ /"; 
      $replacecateg ="-"; 
      $categreplace = preg_replace ($findcateg, $replacecateg, $categ); 
      header('Location: /categ/$rowc->catid/$categreplace');
      exit;
      }
    }
    else
    {
    header('Location: /error');
    exit;
    }
  }
}
I have tried this. It's not keen.
Our guys have to change categories names, or move prods from one to another, and then Google finds a page cached, and hey presto, a soft 404 error.

I want to wipe them out, dynamically.

Re: How do you enter a 301 redirect in PHP?

Posted: Fri Jun 13, 2014 8:15 am
by anant123
header("location:../../index.php");