Apache .htaccess: ErrorDocument and RewriteEngine not working together - php

Please take a look at the following .htaccess
ErrorDocument 404 /404/
RewriteEngine On
RewriteRule (.*) index.php [L]
With this setup, I am using header('HTTP/1.1 404 Not Found'); in PHP to redirect to the error handling page and send the appropriate HTTP status code. The correct 404 status code is sent, but the browser shows a blank page and the access log shows "GET /invalid-url/ HTTP/1.1" 404 -
Can anyone tell me how to make ErrorDocument work with Apache URL rewrites?

Yes declare 404 document after the Rewrite rules
It's normal your server do not push any 404 error, you're using a catchall regexp ((.*)) as the only rewrite rule.
But your issue is not really htaccess related.
In php if you send 404 header the browser wont be redirected to the 404 page automaticaly, but you have to serve error page content yourself in PHP, as it is done by most frameworks with internal routing system.

I guess the answer to my question is: I don't need to.
What I was trying to accomplish was a "404 redirect". That is, when requesting an invalid URL, redirect to the 404 document along with setting the "Status 404 Not found" header.
And I am not sure that's something I want, because invalid URLs should be marked with the 404 status code, not redirected.

It should work if you define the Rewrite before the ErrorDocument.

Try this
ErrorDocument 404 /404.html
RewriteEngine On
RewriteRule (.*) index.php [L]

Related

How 404 page will work

This is my error page i dont know exactly what is but i want to redirect this kind of error to 404.html page:
I have created a 404 error page name "404.html". I want to redirect to this if no file or folder exist in my server.
Where do I have to place this page (404.html)?
Is it possible to redirect this page in .htaccess (or possible with only .htaccess)?
If is there anything more about 404 page let me know, I want to grab the knowledge.
And don't vote me down if possible of copy!
I want to map one error document file for 404 500 i am trying with this code.
ErrorDocument 404 /404.html
ErrorDocument 500 /404.html
RewriteEngine On
RewriteBase /
# map them to one error document:
RewriteCond %{REQUEST_URI} ^/404/$ [OR]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /404.html [L]
I tried this code and uploaded the file in main folder www.website.com/404.html
Its easier then I expected just gave full url:after reading documentation.and it worked.
ErrorDocument 404 http://www.website.com/404.html
ErrorDocument 500 http://www.website.com/404.html
No answer needed.
Where you want on your server
Yes : ErrorDocument 404 /yourPath/404.html
Google and Apache docs are your friends.
Edit : You add an image on your question. This is a 500 Internal Server Error, and not a 404 Page not Found error. Handle it with
ErrorDocument 500 /yourPath/500.html
But if your .htaccess file is corrupted and cause this error, you first have to fix your .htaccess file.
You can specify the document for error handling inside .htaccess similar to one below
ErrorDocument 404 /404.htm
This will override apache's default 404 page. The 404.htm will be inside your root folder.
https://www.addedbytes.com/articles/for-beginners/error-documents-for-beginners/

htaccess not redirecting to custom 404 page

I have a custom 404 page called 404page.php , and I have the htaccess file with the line that I found should work (this is the only line I have in the .htaccess file)
ErrorDocument 404 /404page.php
It was working fine yesterday, but today I was optimizing images and some other stuff on the website, and when I reuploaded it, the htaccess didn't redirect to 404page.php anymore, but to a page that has this written:
Not Found
The requested URL /asfsdtg was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
(I just typed asfsdtg to test if it was working).
The website is uploaded online and I'm managing it through cpanel (I'm still a beginner). I searched for some solutions, I tried adding another line to .htaccess, then it looked like this:
RewriteEngine on
ErrorDocument 404 /404page.php
I even tried putting the location of the 404page.php as a local link, and the internet link, and it still gave me the weird error page.
Does anyone have some idea whats happening? If you need more info that I didn't supply please tell me what more I can supply
Try out this:
RewriteEngine on
ErrorDocument 404 http://yoursitename.com/404page.php
And be sure that 404page.php exists on the root of your server and is trully called 404page.php not 404Page.php
Be carefull at the characters, this is key sensitive!
Try with like this:
RewriteEngine on
ErrorDocument 404 http://www.sitename.com/404.php
Make to handle any 404 errors.
ErrorDocument 404 /404page.php
If above setting does not works you have to write addition line of code as mentions below.
Which identifies for non existing files and directories and redirect to 404.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ - [R=404,L]

000webhost.com Custom Error page does not work?

When i change the code and add something new it doesnt even lets me load my normal page. If delete the ErrorDocument 404/error.php(and the others) I can normally enter my site.
Here is my sites domain: carnageband.tk
Here is the code:
# Do not remove this line, otherwise mod_rewrite rules will stop working
RewriteBase /
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php
What should i do to bypass the page which it redirects me? LINK
I just had the same problem and the issue seemed to be coming from the .htacess file. The webpage would load okay again after I deleted the .htaccess file.
I was able to fix it by editing the .htaccess file from the cPanel, File Manager instead of transferring the file over FTP, but I'm not really sure why it wasn't working before.
My code looks similar to yours:
# Do not remove this line, otherwise mod_rewrite rules will stop working
RewriteBase /
ErrorDocument 403 /errors.php?p=403
ErrorDocument 404 /errors.php?p=404
ErrorDocument 500 /errors.php?p=500

i want to output on 404 page the url the user was trying to use that resulted in the 404 page

I'm trying to create a custom 404 page to include this information:
Sorry, this page was not found
The page you was trying to view was:
Please check the URL is spelt correctly, if it is then this page may
have been moved, removed or never existed
I have tried using: $_SERVER['HTTP_REFERER'] to capture the URL that the user was trying to view before they got automatically redirected to the 404 page
working .htaccess code
Options -MultiViews
RewriteEngine On
ErrorDocument 500 /500.php
ErrorDocument 404 /404.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteRule ^(.*)$ $1.php [L,QSA]
It's working now, thanks everyone who helped me
You want to use $_SERVER['REQUEST_URI'] which gives you the URI which was given to access the current page. The result is in format /request_uri.php. Please refer to http://php.net/manual/en/reserved.variables.server.php for further details.
In your 404.php you could do the following:
The page you were trying to view was: <?php echo $_SERVER['REQUEST_URI']; ?>
Update:
Update your ErrorDocument declaration as follows:
# Remove http://www.example.com
ErrorDocument 404 /404.php
From the docs (http://httpd.apache.org/docs/2.2/mod/core.html#errordocument)
Note that when you specify an ErrorDocument that points to a remote URL (ie. anything with a method such as http in front of it), Apache will send a redirect to the client to tell it where to find the document, even if the document ends up being on the same server. This has several implications, the most important being that the client will not receive the original error status code, but instead will receive a redirect status code. This in turn can confuse web robots and other clients which try to determine if a URL is valid using the status code. In addition, if you use a remote URL in an ErrorDocument 401, the client will not know to prompt the user for a password since it will not receive the 401 status code. Therefore, if you use an ErrorDocument 401 directive then it must refer to a local document.

htaccess ErrorDocument 404 redirect not working

Please tell me why this is not working. It was working but has inexplicably decided to stop working.
In php I issue a 404 if the page is not found like so.
if(checkPageExists($escaped_url_page_name)){
header('HTTP/1.1: 200 OK');
}else{
header("HTTP/1.0 404 Not Found");
die;
}
My htaccess is
php_flag magic_quotes_gpc Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
ErrorDocument 404 /404.php
The 404 page is on the root and is name 404.php. Why would that not work when five minutes ago it was and nothing has changed. I have tested the page with fiddler and it is indeed throwing the 404's but the redirect is not happening. I have friendly urls implemented, but I would not think this affects it as the "/" in the htaccess refers to the physical root of the site I thought. Thanks in advance.
I had a similar issue once - when PHP throws a 404 header, Apache generally won't make a 404 page for it. (Edit: It probably has to do with whether PHP outputs any text. I haven't really experimented with it much.) If you check the headers in your browser, you can see that the 404 header worked, even if the page is blank.
I'm not sure about an automated solution, but I just made an http_error($code) function that prints my custom error page and called that along with header() whenever my code manually triggered a 404. Then I bound a bit of code to ErrorDocument that calls http_error only:
ErrorDocument 404 index.php?http_error=404
ErrorDocument 403 index.php?http_error=403
...and so on.

Categories