Redirection to error 404 page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danaketh
Forum Newbie
Posts: 2
Joined: Mon Jul 14, 2008 9:30 am

Redirection to error 404 page

Post by danaketh »

Hi,

I have simple problem. I want users to be redirected to Error 404 page if they request non existing page (not a file). For example: user wants to get biography of his favourite actor, and because he does not want to click through the site, he simply write http://www.example.com/?actor=my-actor&page=biography into address line. That's ok. But when he makes a mistake and write http://www.example.com/?actor=my-actor&page=biogrpahy or so, I want him to be redirected to Error 404 page.

It's really not a problem for user, because header are not important for him but I want the same for robots. So it have to be correct way - robot must know that he crossed non existing page and get proper code.

Suddenly, I don't know how to solve it :( Maybe it's just because I'm really tired today ;)

Thanks for help...
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Re: Redirection to error 404 page

Post by Jaxolotl »

you may use .htaccess file to redirect them

ErrorDocument 404 http://www.my_site.com/my_404_error_page.php
ErrorDocument 403 http://www.my_site.com/my_403_error_page.php
ErrorDocument 500 http://www.my_site.com/my_500_error_page.php
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Redirection to error 404 page

Post by WebbieDave »

Your example implies that all requests are accessing /index.php?actor=my-actor&page=biography
This way, the web server will never actually send a 404. In this case, you can manually send the 404 through PHP if the lookup returns no results with the header function:

Code: Select all

header('HTTP/1.x 404 Not Found');
danaketh
Forum Newbie
Posts: 2
Joined: Mon Jul 14, 2008 9:30 am

Re: Redirection to error 404 page

Post by danaketh »

WebbieDave wrote:Your example implies that all requests are accessing /index.php?actor=my-actor&page=biography
This way, the web server will never actually send a 404. In this case, you can manually send the 404 through PHP if the lookup returns no results with the header function:

Code: Select all

header('HTTP/1.x 404 Not Found');
Thanks. That's the solution I also figured out after few minutes after I created this topic :) I think that trying to redirect was a bad idea and this is actually the right way. Good to know that I'm going right way :)

Thanks guys!
Post Reply