I need to disable Apache 401 error message or bypass it completely , in case of errors only back end application error codes should be displayed.
Thanks
Rakesh
Add following to your .htaccess file (works if AllowOverride allows it).
ErrorDocument 401 /URL/TO/YOUR/BACKEND/RESPONSE
Custom error documents are configured using the ErrorDocument directive, which may be used in global, virtualhost, or directory context.
More info about Apache ErrorDocument
Related
I wrote on my .htaccess an if to check if the *mod_rewrite* is installed and throw error 500 if not.
ErrorDocument 500 /500.html
<IfModule !mod_rewrite.c>
Error "mod_rewrite not installed"
</IfModule>
If i get an 500 error by it will show the 500.html. But if it gets that same error from the Error directive, it will show the default error page instead. Why ? How do i fix it ?
Well, after thinking a lot about it and reading a bit, i believe that the Error function simply makes the .htaccess parsing to be cancelled - and the error to be logged - and then throw an 500 error from the apache config that has been parsed before this one (for example, an .htaccess from another folder over this one or the httpd.conf from apache)
I have installed apache2 on my ubuntu machine. As I need to work with subdomains I created a proper entry in sites-available which looks like this:
<VirtualHost *:80>
ServerName xxx.localhost
DocumentRoot /var/www/
</VirtualHost>
I also enabled mod_rewrite (and changed "AllowOverride All" in my sites-available/default file) but other than that nothing else was changed.
My .htaccess file does work, and I wanted to handle some error codes. Doing so with 404 worked pretty well, but for some reason other errors don't seem to work. I'm mostly interested in handling error 400:
ErrorDocument 400 /400.php
ErrorDocument 404 /404.php
Is there anything else I should look at? I couldn't seem to find any place where 404 are allowed while other error codes aren't.
If the php is returning the 400 error, then php should generate the error document.
Use something like:
if( $someError )
{
http_response_code(400);
include("{$_SERVER['DOCUMENT_ROOT']}/400.php");
exit();
}
From the Apache documentation:
Although most error messages can be overriden, there are certain circumstances where the internal messages are used regardless of the setting of ErrorDocument. In particular, if a malformed request is detected, normal request processing will be immediately halted and the internal error message returned. This is necessary to guard against security problems caused by bad requests.
Try adding it directly in the httpd.conf and restart Apache.
Problem with custom error pages.
My server root is "C:\www" and error pages are in "C:\www\errors".
So I put a .htaccess file in "C:\www" with following:
ErrorDocument 404 /errors/404.html
ErrorDocument 403 /errors/403.html
ErrorDocument 500 /errors/500.html
This works for anything in "mysite.net". But I also have subdirectories like "first.mysite.net", "second.mysite.net" and so on, and .htaccess from root doest work there. It's searching for "errors" folder in these subfolders and return the default 404 page.
I've tried this:
ErrorDocument 500 C:/www/errors/500.html
ErrorDocument 500 C:\www\errors\500.html
Doesn't work.
ErrorDocument 500 http://mysite.net/errors/500.html
This works, but it makes a redirect and I don't want this. I want the browser to keep adress line the same.
Can anyone make this clear for me? Thanks.
If they are subdomains with seperate virtualhost definitions you need to check virtualhost definitions and AllowOverride settings for those subdomains. See http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride and change AllowOverride setting as needed.
I have the following piece of code in my .htaccess file to force redirecting to custom error pages:
ErrorDocument 404 /ErrorPages/404.php
ErrorDocument 403 /ErrorPages/403.php
ErrorDocument 400 /ErrorPages/generalError.php
ErrorDocument 401 /ErrorPages/generalError.php
ErrorDocument 500 /ErrorPages/generalError.php
Everything works fine on port 80, but when it comes to SSL, the standard error pages are shown.
To be more specific:
http:www.mydomain.com/NoExistingPage.php redirects to the custom error page
https:www.mydomain.com/NoExistingPage.php DOES NOT redirect to the custom error page
am I missing something here?
Thanks in advance
Try putting a duplicate .htaccess file in the /secured folder and see if that works. The vhost for the https daemon is probably using /secured as doc_root which means even if the .htaccess from the regular doc_root is below the secured folder it will be ignored.
I have a number of ErrorDocuments setup in my .htaccess file for errors such as 404, 401, 403 etc which all redirect to my error page but the ErrorDocument set for a 500 error is never displayed when PHP reports a 500. The 500 code is sent to the browser and the output is blank. Is there something special I need to do to enable 500 error documents for use with PHP?
My directives look like this:
ErrorDocument 401 /errorpage.php?error=401
ErrorDocument 403 /errorpage.php?error=403
ErrorDocument 404 /errorpage.php?error=404
ErrorDocument 500 /errorpage.php?error=500
I've looked through the php.ini and can't see anything that would obviously override the Apache settings and there are no ErrorDocument directives in my httpd.conf either. Anywhere else I should be looking?
Thanks in advance.
See this answer to a very similar question. Basically, PHP isn't hardly ever going to trigger a 500.
You may need to add the ErrorDocument declaration earlier in the Apache conf chain. If you add this to a vhost conf it may not be called.
I happened to encounter the same issue while working with codeigniter and Imagick. imagick was setting a 500 HTTP error when something went wrong and in that case Codeigniter's custom 500 message was not displayed.
I resolved this by adding try-catch to all Imagick functions.Check that where from the 500 issue is arising and then add a try-catch there.As for
ErrorDocument 500 /errorpage.php?error=500
,I read loads online about the same where some people claim that this solved their similar issue, others say that it wouldnt help as Apache has handed over the control to PHP.Maybe some php code is setting Headers to 500 and that would probably lead to the browser displaying its custom 500 error message.
I don't believe Apache will let you run PHP files for 500 errors because the error page could generate an error. Try rendering out your 500 error to an HTML file and point your directives at that.