LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-12-2014, 02:09 AM   #1
mitter1989
Member
 
Registered: Sep 2013
Posts: 47

Rep: Reputation: Disabled
Question apache error document


Hi Folks,

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.

Thanks in advance.
 
Old 06-12-2014, 03:21 AM   #2
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by mitter1989 View Post
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.

Does that hint get you anywhere?

[X] Doc CPU
 
Old 06-12-2014, 04:05 AM   #3
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
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:
Code:
RewriteEngine On
RewriteCond %{REQUEST_URI} .php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /path/to/custom-errordoc
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

Regards
 
Old 06-12-2014, 04:17 AM   #4
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by bathory View Post
You can do the above, or use mod_rewrite.
Leave the ErrorDocument on its default setting and use:
Code:
RewriteEngine On
RewriteCond %{REQUEST_URI} .php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /path/to/custom-errordoc
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.

[X] Doc CPU
 
Old 06-12-2014, 04:40 AM   #5
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
Originally Posted by Doc CPU View Post
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).
 
Old 06-12-2014, 05:00 AM   #6
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by bathory View Post
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.

[X] Doc CPU
 
Old 06-12-2014, 05:32 AM   #7
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
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.

Cheers
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Apache cant see document root crowx Linux - Newbie 11 02-09-2011 11:21 PM
apache document not exist error linuxguy08 Linux - Server 3 01-31-2011 07:36 PM
Apache and Firefox "Document contains no data" error. ]SK[ Linux - Software 0 08-16-2005 01:44 AM
Apache: Document Not Found error voyciz Linux - Software 3 06-17-2004 04:51 AM
Document Root in Apache Hal Linux - Newbie 2 10-28-2003 06:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 10:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration