How do you enter a 301 redirect in PHP?
Moderator: General Moderators
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
How do you enter a 301 redirect in PHP?
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?
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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do you enter a 301 redirect in PHP?
I guess this cannot go into an include file, but must be in the index.php template, because it is a HEADER ??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do you enter a 301 redirect in PHP?
That does not follow. The only condition is that headers have not already been sent.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do you enter a 301 redirect in PHP?
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!!
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!!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do you enter a 301 redirect in PHP?
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.
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.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do you enter a 301 redirect in PHP?
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:
.... ??
Then query for the category, if it is queried. Subcategory if queried. And Product.....
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.....
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do you enter a 301 redirect in PHP?
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.
As much as possible, try to keep your business logic out of your presentation to avoid situations like this altogether.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: How do you enter a 301 redirect in PHP?
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;
}
}
}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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do you enter a 301 redirect in PHP?
header("location:../../index.php");