Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am using RHEL 6.4 x86_64 OS and have installed apache/2.2 on it, I have configured custom ErrorDocument and its working fine, now What I want is :
1.) Show this page(custom ErrorDocument) only when someone request for a page called *.php page and that does not exist on the server for ex. : www.example.com/abcd.php
2.) If someone request for *.html, *.jpg and that does not exist on the server then no response should be given or other Document Error messge should displayed for ex. : www.example.com/abcd.html and www.example.com/abc.jpg
I am using RHEL 6.4 x86_64 OS and have installed apache/2.2 on it, I have configured custom ErrorDocument and its working fine, now What I want is :
1.) Show this page(custom ErrorDocument) only when someone request for a page called *.php page and that does not exist on the server for ex. : www.example.com/abcd.php
2.) If someone request for *.html, *.jpg and that does not exist on the server then no response should be given or other Document Error messge should displayed for ex. : www.example.com/abcd.html and www.example.com/abc.jpg
How can I achieve this? Kindly guide me.
from what you describe, it is obvious that we're talking about the 404 ErrorDocument (but it wouldn't have hurt to say so explicitly). And you want different error documents depending on what kind of resource was originally requested. Correct?
Well, you can't have more than one error document for the same status, but there's a way around that. The error document can itself be a PHP script, and then it can look at $_SERVER['REQUEST_URI']. This variable holds the name of the resource that was initially requested, and depending on that, the script can echo different contents.
from what you describe, it is obvious that we're talking about the 404 ErrorDocument (but it wouldn't have hurt to say so explicitly). And you want different error documents depending on what kind of resource was originally requested. Correct?
Well, you can't have more than one error document for the same status, but there's a way around that. The error document can itself be a PHP script, and then it can look at $_SERVER['REQUEST_URI']. This variable holds the name of the resource that was initially requested, and depending on that, the script can echo different contents.
You can do the above, or use mod_rewrite.
Leave the ErrorDocument on its default setting and use:
So whenever a .php doesn't exist you get your custom error page, and when a .html or whatever doesn't exist, you get the default apache 404 response
true, I didn't think of that. But your proposed solution has a serious flaw: If the RewriteRule matches, the so-called custom-errordoc is sent with a status code of 200 (OK) instead of 404 (Not Found). Which is logical, because from Apache's view, the document was retrieved successfully after following some internal rules, but it's technically wrong.
true, I didn't think of that. But your proposed solution has a serious flaw: If the RewriteRule matches, the so-called custom-errordoc is sent with a status code of 200 (OK) instead of 404 (Not Found). Which is logical, because from Apache's view, the document was retrieved successfully after following some internal rules, but it's technically wrong.
Note that all 3 conditions must be met before apache gives the custom-errordoc in response to a 404 error.
So if a client asks for the custom-errordoc, one condition is not met (the file exists), the rewrite does not happen, but the client still gets the custom-errordoc (status code 200).
Note that all 3 conditions must be met before apache gives the custom-errordoc in response to a 404 error.
So if a client asks for the custom-errordoc, one condition is not met (the file exists), the rewrite does not happen, but the client still gets the custom-errordoc (status code 200).
guess you misunderstood me. Let's assume a client requests /not-there.php an look what happens:
* first condition is met, the request ends in .php
* second condition is met, there is no file of that name
* third condition is met, there is no directory of that name
So the RewriteRule is applied and Apache serves /path/to/custom-errordoc as desired. But -and that's my issue of criticism- it does so with a status code of 200, not 404 as would be correct. Using the ErrorDocument directive, however, the 404 status is being preserved and the client gets that status along with the ErrorDocument.(*) Of course, if the ErrorDocument itself is a PHP script, you can always force the HTTP status to 404 using header().
(*) It's a frequent mistake to specify the ErrorDocument as an HTTP resource instead of just a file. If you do so, Apache replies with a Redirect (301 or 302, can't seem to memorize which is which) and instructs the client to retrieve the ErrorDocument with a subsequent direct request. That way, the actual error condition is lost, too - there's just the 30x, and then a 200.
guess you misunderstood me. Let's assume a client requests /not-there.php an look what happens:
* first condition is met, the request ends in .php
* second condition is met, there is no file of that name
* third condition is met, there is no directory of that name
So the RewriteRule is applied and Apache serves /path/to/custom-errordoc as desired. But -and that's my issue of criticism- it does so with a status code of 200, not 404 as would be correct. Using the ErrorDocument directive, however, the 404 status is being preserved and the client gets that status along with the ErrorDocument.(*) Of course, if the ErrorDocument itself is a PHP script, you can always force the HTTP status to 404 using header().
Ah, ok now I see what you mean.
Anyways let OP decide what fits his needs.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.