I was after a way to create a custom 404 page, that would redirect the user without changing the URL address bar. For example, if someone entered www.mysite.com/articles/php_rocks, and I was using the header() function to redirect to index.php?id=1, the user would see the right page, but the URL address would be changed.
I wanted the same effect, but without the URL rewrite, so the user would still see the php_rocks URL, while the browser displayed the content from the index.php?id=1 page.
The first step is to edit the .htaccess file to include the line ErrorDocument 404 /not_found.php
You then need to write a not_found.php that looks through the supplied URL string to search for certain identifiers. In my case, the word /articles/. Having identified that, the rest of the string (eg 'php_rocks') is used to search the database for an article with the same name. If it finds it, the user can then be redirected.
Here's the problem I had ... you can't supply variables into include(), which means include("index.php?id=1") won't work. You can, however, do the following
Code: Select all
$_GET['id']=1;
include("index.php");The next problem I found was that any calls to include() from index.php were being ignored, as the script was attempting to run them from the imaginary /articles/ directory. The way to solve this is to add the line <base href="http://www.mysite.com" /> into the meta data at the top of the page.
Job done, one "silent" redirect using PHP. I hope this makes sense. Any questions, please ask!