Forum Moderators: phranque

Message Too Old, No Replies

htaccess ErrorDocument 404 returning Status 200 OK

.. should be returning Status 404 Not Found

         

CalmBlueRage

3:31 pm on Apr 5, 2010 (gmt 0)

10+ Year Member



Hello,

I've been researching and trying ways to get my .htaccess file's ErrorDocument 404 to return the proper HTTP Status when using a custom 404 page. So far, I'm only able to generate the HTTP/1.1 200 OK header.

I noticed that calling missing .cfm files will result in a standard, server 404 error message, with proper status headers. All other file types, .html, .htm return my custom 404 page with the 200 OK status.

Any guidance would be greatly appreciated. I've included the htaccess file below.


--- Begin .htaccess ---

<Files .htaccess>
order allow,deny
deny from all
</Files>

Options -Indexes

RewriteEngine on
Options FollowSymlinks
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.cfm\ HTTP/
RewriteRule ^index\.cfm$ http://www.example.com/ [R=301,L]

ErrorDocument 404 /404error.cfm


--- End .htaccess ---

[edited by: jdMorgan at 10:08 pm (utc) on Apr 5, 2010]
[edit reason] example.com [/edit]

g1smd

5:10 pm on Apr 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As an aside, the index rule should be listed before your general canonicalisation rule.

At the moment a request for exactly
example.com/index.cfm
will be unnecessarily redirected twice through an unwanted redirection chain.

Additionally, the rules could do with some modifications: especially tidy the casing of the instructions, escape the literal periods in patterns, and then change
rewritecond %{http_host} ^domain.com [nc]
to be
RewriteCond %{HTTP_HOST} !^(www\.domain\.com)?$
instead.

CalmBlueRage

9:05 pm on Apr 5, 2010 (gmt 0)

10+ Year Member



Thanks for the reply. I will try to work through it.
Ok... I've modified the the htaccess file, however now non-www domain does not redirect to www.domain. The custom 404 page continues to return a HTTP Status 200 OK


<Files .htaccess>
order allow,deny
deny from all
</Files>

Options -Indexes

RewriteEngine on
Options FollowSymlinks
RewriteRule ^index\.cfm$ http://www.example.com/ [R=301,L]
RewriteCond %{HTTP_HOST} !(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.cfm\ HTTP/


ErrorDocument 404 /404error.cfm

[edited by: jdMorgan at 10:07 pm (utc) on Apr 5, 2010]
[edit reason] example.com [/edit]

g1smd

9:22 pm on Apr 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The condition and rule order has been mixed up.

There's a pair for the index redirect - and those should be first.
The last pair should be for the non-www to www general redirect.

Add a #comment before each block of code so you know what it does when you read the file again months or years later.

jdMorgan

10:04 pm on Apr 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aside from the scrambled rules, the problem is likely in the 404error.cfm script itself, since your ErrorDocument directive is correct and neither of your mod_rewrite rules should affect requests for missing documents or for the resulting /404error.cfm file access.

Make sure you are using a headers checker that will show *all* HTTP transactions between the client and the server -- for example, if there is a redirect occurring before /404error.cfm is served in response to a request for "www.example.com/no-such-file.html" (a non-existent page in the correct domain), then that is a problem.

Jim

CalmBlueRage

1:20 pm on Apr 6, 2010 (gmt 0)

10+ Year Member



Using the Firefox add-on "Live HTTP Headers", I was able to see the status 200 response. I've commented out the ErrorDocument line until I can sort through the reasons why 404 isn't being returned.

Thanks for your help.

jdMorgan

2:38 pm on Apr 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might want to disable MultiViews and --if on Apache2.x-- AcceptPathInfo is your site does not depend on them to function. Either of these can take precedence over mod_rewrite and cause 'weird' behavior.

Change your existing Options directive as shown and consider using the first line as well if you're on Apache2.x:

AcceptPathInfo Off
Options -Indexes -MultiViews

Many hosts enable MultiViews by default, even though it's a timer-waster if you don't need it for content-negotiation.

Jim