Forum Moderators: coopster

Message Too Old, No Replies

404 redirect script not working

404, redirect, script

         

galahad2

8:56 am on Sep 17, 2009 (gmt 0)

10+ Year Member



Hi, I've set up a PHP script which checks the inputted URL against records in a db, and does one of two things- (1) sends the user to a specified URL if the typed URL matches a record in the db, or (2) just outputs the 404 ewrror page if there's no match in the db.

The problem is that it always outputs the 404, regardless of whether or not there's a matching record in the db. I guess something must be wrong with the script but I can't see what:

This is the code:

<?php
include ('manager/exhibitions/inc/dbconnect.php');

$query = "SELECT showname FROM exhibitionstable WHERE showname = '". mysql_real_escape_string($_SERVER['REQUEST_URI']) ."' ";

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

if ($numrows == 0)
{
echo "<p>404 error</p>";
}
else
{
header('location: exhibitions.html');
exit;
}
?>

Any ideas?

barns101

1:15 pm on Sep 17, 2009 (gmt 0)

10+ Year Member



Your query could be failing. Try the following, removing a space at the end:

"SELECT showname FROM exhibitionstable WHERE showname = '". mysql_real_escape_string($_SERVER['REQUEST_URI']) ."'";

Also try adding "or die(mysql_error())" as follows:

$numresults = mysql_query($query) or die(mysql_error()); 

galahad2

1:41 pm on Sep 17, 2009 (gmt 0)

10+ Year Member



I've created a variable for the SERVER global, so the whole script now is as below- but it's getting this strange output (and fixed.htc is not even in the database so no idea where it comes from):

SELECT showname FROM exhibitionstable WHERE showname = 'fixed.htc'

This is the code:

<?php
include ('manager/exhibitions/inc/dbconnect.php');

$name = basename($_SERVER['REQUEST_URI']);
if(false!==strpos($name,'?'))
{
$name = substr($name,0,strpos($name,'?'));
}

$query = "SELECT showname FROM exhibitionstable WHERE showname = '". mysql_real_escape_string($name) ."' ";

echo $query;

$numresults=mysql_query($query) or die(mysql_error());
$numrows=mysql_num_rows($numresults);

if ($numrows == 0)
{
echo "<p>404 error</p>";
}
else
{
header('location: exhibitions.html');
exit;
}
?>

What's interesting if that it worked ONCE, I typed in [mydomain...] and travejl.html is an entry in the db) and it forwarded to exhibitions.html.

But now it just shows the output as above...

galahad2

1:42 pm on Sep 17, 2009 (gmt 0)

10+ Year Member



Also just to add it's echoing the "404 error" as well so looks like it's not checking the db proeprly...

jdMorgan

2:16 pm on Sep 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Be very sure (once you get this script logic problem worked out) that it outputs a 301 redirect to the proper page if it exists, or that it outputs a proper 404-Not Found Header (status code) if not.

You can use the "Live HTTP Headers" add-on for Firefox/Mozilla browsers or a similar add-on to verify this.

Also be sure that for any 'good' URL, the server outputs only a single response, and not a 404 followed by a 301. These responses need to match the HTTP protocol requirements, or your search rankings will very likely suffer.

Jim