Kyle Unrau Kyle Unrau - 4 months ago 7
PHP Question

Rewrite all pages not found to another URL whilst keeping real pages without sending a 404 error

I am looking for an if script to tell if a page is found or not found in the .htaccess file! It may seem odd but im sure it can work some how.

Ill show you what im tying to do with examples, in 1 folder I want to have two files, Cheese.php and Pizza.php! When they are located inside the folder I want it so if they go to Cheese.php it will act normal but if they land on a page that does not exist in the folder it will go to Pizza.php, but Pizza.php is not a 404 error message but something else handled by php.

You may just say use the 404 DocumentError, but the only problem is that on internet explorer it does not get Pizza.php but displays its own 404 message and on most other browsers it shows an error in the console...

My request seems weird but hopfully this is posible! Thanks

Answer

You may just say use the 404 DocumentError, but the only problem is that on internet explorer it does not get Pizza.php but displays its own 404 message and on most other browsers it shows an error in the console...

The reason you get the error is that the 404 response code is set by your server when the error document is sent. In .htaccess, do:

ErrorDocument 404 /Pizza.php

In Pizza.php, set the response code early in the script, before you output any content to the browser.

<?php
   http_response_code(200); //tells browser that everything is OK
   ...

Because you've set the response code yourself, your server will not set a 404 and the browser will not know that there is any problem.

Comments