Can we send 404 error code in place of 403 forbidden in request header - http-headers

I want to send the 404 error code when users try to access 403 forbidden directories.
ErrorDocument 404 /404/
ErrorDocument 403 /404/
This sends users to the custom 404 page I have set up, but when I check the request headers, it's still sending a 403 error code. Can I pass a 404 error code through the .htaccess file on forbidden directories?

Related

Apache server status codes

Is there a way, on apache server, to make a specified page, to send code 404 in browser?
I work on an application, where if you tap a url that is not recognised, the application sends code 302, and makes a redirect instead of a page error.html who send code 200 which is normal because is a page who exist.
I need to modify the code for page error.html in 404 from httpd.conf.
Can you help me please?
I've tried with ErrorDocument 404 /error, but the application does not send 404, it sends 302. I need to rewrite this 302 in 404.
create an error page 404.html on the httpd DocumentRoot.
Add ErrorDocument 404 /404.html in httpd.conf.
Restart Apache and access a page that does not exist.

htaccess - return 404 instead of 403

I'm running a webserver with apache2 on raspbian.
On /var/www/html/resources/, there are some files (with e.g. usernames, passwords) but I dont want to let anybody know that they exist, so I want to return a 404 (not found) status code instead of a 403 (forbidden).
(A 403 error would tell anybody that he is on the right way...)
Yes I have read many questions like this, and most of the links below this question is exactly what I need but it didn't work for me.
Do I have to change something in a file (httpd.conf,...)?
Here are all my attempts to return a 404 instead of 403.
If I try:
## return 404 for each .htaccess
RedirectMatch 404 ".*\/\..*"
in /var/www/html/.htaccess, I get a 403 instead of a 404 like I would expect, if I visit //localhost/.htaccess.
RewriteEngine On
RewriteRule ^.*$ /404 [L]
Results in a Internal Server Error (500)
If I tried this:
# Will raise a 404 error, because the file <fake_file_for_apache_404.php> doesn't exist.
# We change 403 to 404 !
ErrorDocument 403 /fake_file_for_apache_404.php
# We need to rewrite 404 error, else we will have "fake_file_for_apache_404.php not found"
ErrorDocument 404 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL <script type=\"text/javascript\">document.write(document.location.pathname);</script> was not found on this server.</p></body></html>"
I got a 403 in combination with a 404 (403 - the file I wanted to access was forbidden, 404 - the file I should become redirected was not found.)
So what am I doing wrong?
You can make it in Perl. So, in the secret directories, in .htaccess:
ErrorDocument 403 /cgi-bin/fake404.cgi
The mentioned /cgi-bin/fake404.cgi:
#!/usr/bin/perl -w
use strict;
print 'Status: 404 Not Found
Content-Type: text/html; charset=UTF-8'."\n\n";
print '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
'."<p>The requested URL $ENV{REQUEST_URI} was not found on this server.</p>\n</body></html>\n";
I tested the above Perl code: it works as expected.
P. S. And for secret files in non-secret directories, use the same directive in .htaccess-es, but in a <Files> of <FilesMatch> block.

Reset/Remove ErrorDocument via htaccess

I use host that has custom ErrorDocument settings already set in their httpd.conf and I can't edit this file (I don't have access) to solve the problem directly there.
Some background: I use an API that when something isn't right I manually set a responde code and print a json as error response to use in client side. Since I am triggering a response code (400 - Bad Request in my case) it falls directly in the catch of my Ajax request.
Their configuration for a custom 400 error page is causing this issue, the response of my API never returns because i got as response their custom page.
Is it possible to remove/reset or set it to show the page that triggered the error of an already set ErrorDocument via .htaccess?
On server where virtual server settings cannot be modified but .htaccess is accessible, this works for me:
ErrorDocument 401 default
ErrorDocument 402 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 500 default
ErrorDocument 501 default
ErrorDocument 502 default
ErrorDocument 503 default
ErrorDocument 504 default
Put it into .htaccess and it will reset error handling pages.

Why Would Requesting a Nonexistent Image Generate a 302 Response Instead of 404?

I have a website and I have configured my main .htaccess file with this directive:
ErrorDocument 404 https://websiteLink.com
When I access a page that has a link to a nonexistent image, instead of receiving a the 404 above, I receive a 302 response with the location directive that specified in the .htaccess configuration.
Why this 302 response generated?
This is because of the absolut url. You are using a full url in your ErrorDocument's destination. Use an absolute path
ErrorDocument 404 /path/

Change Default Error Catch in htaccess?

So my question is similar to the question posed in this page
Single ErrorDocument directive to catch all errors (.htaccess)
The above person was trying to get a dynamic error page.
However my question is slightly different (although it may still have the same answer).
I am with a shared web host who displays custom error pages by default with ads on it. I want errors to result in a real error response without ads. Do I need to individually do a:
ErrorDocument 404 /404.html
For each error? Or is there a way to just tell it to show a normal error message for all errors?
Is there some type of:
ErrorDirective JustOutputErrorInsteadOfRedirecting
Thank you in advance.
With ErrorDocument directive you can also display Your custom error messeage on the same page.
ErrorDocument 404 "Sorry, the page does not exist!"
ErrorDocument 403 "Ohh, you don't have permission to access this page!"
ErrorDocument 410 "Sorry, the page no longer exists"
You can also format these messges using html tags
ErrorDocument 404 "<h2>Sorry, the page does not exist!</h2>"
ErrorDocument 403 "<h2 style='color:blue'>Ohh, you don't have permission to access this page!</h2>"
ErrorDocument 410 "<p>Sorry, the page no longer exists</p>"
The message will appear on the same page.