Jump to content
JesseElser

404 Redirect Not Working

Recommended Posts

So while editing my .HTACCESS file I added the proper lines to redirect users that encounter errors like 404 and 500. It works like a charm if I tell it to display a specific message. However if I tell the file to redirect users to a custom error page it fails.

When testing out my 404 redirect I get this:

Not Found

The requested URL /143/test.php was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Apache/2.4.9 (Win64) PHP/5.5.12 Server at localhost Port 80

As you can see it is stating that my ErrorDocument is not found BUT if I type my ErrorDocument URL in I can go to it myself.

My .HTACCESS file looks like this:

ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php
ErrorDocument 502 /error.php
ErrorDocument 504 /error.php

# supress php errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off

# enable PHP error logging
php_flag log_errors on
php_value error_log PHP_errors.log

NOTE: I am using localhost (WAMP).

Thanks.

 

Share this post


Link to post
Share on other sites

Is it your application that determines whether it's a 404 page, or the webserver itself? Like for your /143/test.php example, is there an actual directory named 143 with a test.php file in it, or does your app take the request, translate it and look it up in a db for routing or something? If it's the app making the determination, then those ErrorDocument  directives will never be used as those are for "native" errors to the webserver.

 

This might be a language problem around the term "redirect", but you should never "redirect" to a 404 page. Redirects change the url and actually take the visitor to a different page. You should just throw a 404 status header and display a custom not found message, but the URL should still be the actual page that is missing and not redirected (a new request) to /errors.php. Otherwise, you are telling browsers and search engines that "http://yoursite.com/errors.php" doesn't exist instead of the actual missing page, which in fact it does.

Share this post


Link to post
Share on other sites

I found a solution but I appreciate the replies.
First: CroNiX test.php was added to the url to trigger the 404 error since that file does not exist.

It turns out that in the .htaccess file I had the urls wrong since I did not realize that it was looking at the root folder. This was solved by simply changing the paths from ErrorDocument 404 /error.php to ErrorDocument 404 /143/error.php

 

Marked as "Best Answer" for anyone else that made a similar mistake.

Edited by JesseElser

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.