Runilo Posted March 30, 2007 Share Posted March 30, 2007 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? Quote Link to comment Share on other sites More sharing options...
Full-Demon Posted March 30, 2007 Share Posted March 30, 2007 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 Quote Link to comment Share on other sites More sharing options...
Runilo Posted March 30, 2007 Author Share Posted March 30, 2007 ok, thanks. So the problem is that i have a mysql query before sending the header? Quote Link to comment Share on other sites More sharing options...
Runilo Posted March 30, 2007 Author Share Posted March 30, 2007 Also, is this a safe method regarding google and other search engines? Quote Link to comment Share on other sites More sharing options...
Full-Demon Posted March 31, 2007 Share Posted March 31, 2007 Yeah, its probably the mysql query, or whatevr you placed in db.php. Not sure what you mean, Im not very familiar with search engine bots, if thats what you mean... Full-Demon Quote Link to comment Share on other sites More sharing options...
neel_basu Posted March 31, 2007 Share Posted March 31, 2007 ok, thanks. So the problem is that i have a mysql query before sending the header? Try Removingheader("HTTP/1.1 404 Not Found"); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.