Pregunta

I've set 404 error page in .htaccess and it works fine.

ErrorDocument 404 somedir/404.php

Now I'm trying to figure out how to show that page when user with no privileges tries to access some pages.

Ok, what have I tried so far?

Logic says if I redirect user to some non-existing page that will trigger 404 server response, or I can just redirect him to 404.php. Correct, that works.

header ("location: /404.php");
exit;

That way URL changes from one, user asked for, to (in this example) http://www.mydomain.com/404/php. I don't want to change it! I want user to see address he requested, no matter is that page protected or doesn't exist at all.

Course, I can work around and load 404.php but that makes headache to me because of way how its template engine works. There's entire philosophy of how that tplEngine controlls templates and I'll leave that as "Plan B" if needed.

Is there any easy way to do that with no complicated work around?

¿Fue útil?

Solución

You can change the document header to show a 404 error.

header('HTTP/1.0 404 Not Found');

After that, you can load the 404.php page with file_get_contents(). The URL will stay the same.

Otros consejos

Instead of header function you can include 404.php and return custom status:

include ( "404.php" );
exit;

And from 404.php have this:

http_response_code( 404 );
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top