Jump to content

Can't get header redirect code to work


Runilo

Recommended Posts

Hello

 

I'm using mod_rewrite on a website that i'm developing, and i want a handler on my index file which redirects to a 404 header if the content is not found.

I've written this code that's suposed to redirect to a 404 header, if the content is not found:

 

Example URL: www.example.com/funny-videos/ass-catches-on-fire

 

<?php

include("db.php");

$url_request = $_SERVER['REQUEST_URI'];

$url_array = explode ("/", $url_request);

$category = $url_array[1];

$media = $url_array[2];

if (!empty($category))

{

if ($category == "funny-videos")

{

$db = "filmar";

}

else

{

if ($category == "funny-pictures")

{

$db = "myndir";

}

else

{

if ($category == "free-wallpapers")

{

$db = "wallpapers";

}

}

}

if ($db == "filmar" OR "myndir" OR "wallpapers")

{

$result = mysql_query("SELECT * FROM $db WHERE trim = '$media'") or die(mysql_error());

while($row = mysql_fetch_array( $result )) {

if (!$row['trim'] == $media)

{

header("HTTP/1.1 404 Not Found");

header("Location:". $_SERVER['REQUEST_URI']);

exit();

}

}

}

}

include("config.php");

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>

<head>

As you can see, the code checks a part of the url and matches it with a column in the database, and if the url does not match anything in the database, it's suposed to refresh the site with the same url, only this time with a 404 header, but it doesn't work.

I don't get anny errors or anything, but i just don't get a 404 header no matter the url.

 

Anyone know how to solve this?

Link to comment
Share on other sites

Headers are send at the beginning of a page. You cant just use it somewhere in the middle. Before you output anything (including mysql queries and session/cookie things) you can set the header.

 

I use JS in the middle of the page to redirect the user:

 

die ('<script language="javascript" type="text/javascript">document.location="index.php";</script>');

 

(remember this is php, die is the same as echo command, but php will stop doing anything after die)

 

Full-Demon

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.