Forum Moderators: phranque

Message Too Old, No Replies

my apache only accepts .htm for custom 404 pages

I can't use .php (on wamp)

         

costamatrix

1:43 pm on Sep 29, 2009 (gmt 0)

10+ Year Member



Hi, i have created a custon 404 page..its is called /404.php

my .htaccess file:

RewriteEngine On
RewriteRule ^(.*)\/(.*)\/(.*)?$ /$1.htm [E=rwdone?yes,L]

ErrorDocument 404 /404.php

when i retrieve a page that does not exist, a normal 404 error is showed...but if i change the .htacces to:

RewriteEngine On
RewriteRule ^(.*)\/(.*)\/(.*)?$ /$1.htm [E=rwdone?yes,L]

ErrorDocument 404 /404.htm

and rename my 404.php to 404.htm it works...the php commands are showed like it was text, but the redirect at least works...

what i have to do to be able to use a custom 404 page dinamic? (php)

costamatrix

2:33 pm on Sep 29, 2009 (gmt 0)

10+ Year Member



i have tried alter the .htacces to:

Options +Includes

RewriteEngine On
RewriteRule ^(.*)\/(.*)\/(.*)?$ /$1.htm [L]

ErrorDocument 404 /404.shtml

-----------------------------

the file worked but my php code was not executed (apache understands the file like a plain text file);;..

so i add the line:

AddType application/x-httpd-php .shtml

now apache does not use my 404 file and show the default page...

jdMorgan

3:25 pm on Sep 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's obvious that you are trying to 'guess' a solution here. Don't do that, as you may do great harm to your site.

You want /404.php as your 404 errordocument? Then you must use "ErrorDocument 404 /404.php"

If it shows as plain text, that means that you have not told Apache to execute it server-side instead of just serving it as a "text file." So you will need to try both

 AddType application/x-httpd-php .php 

or
 AddHandler server-parsed .php 

If your code is in .htaccess, you also need to prevent an 'infinite' rewriting loop:


RewriteCond %{REQUEST_URI} !\.htm$
RewriteRule ^([^.]+)\/([^/]+)/([^/]+)$ /$1.htm [L]

Note that the rule pattern shown is *much* more efficient than the one you used, but should be practically equivalent.

Jim

costamatrix

4:55 pm on Sep 29, 2009 (gmt 0)

10+ Year Member



thx Jim....

my apache are executing php pages normally....but when i tried to use a php page as errordocument 404, apache just ignores it and show its default 404 page...i tried to use a 404.htm page instead of 404.php and then the my was used as error document...

it appears to me that somehow apache are blocking the use of php document on error document 404, because when i tried to tell apache to process .htm extension like a .php extension, he just ignores my error page and show the default on.

can you help -me?

costamatrix

5:15 pm on Sep 29, 2009 (gmt 0)

10+ Year Member



i increased the error log level to debug and discover that my php erro page had a syntax error,,, but even transforming my page to pure html, it cannot work...see:

my .htacces:

RewriteEngine On

RewriteCond %{REQUEST_URI} !\.htm$
RewriteRule ^([^.]+)\/([^/]+)\/([^/]+)$ $1.htm [L]

ErrorDocument 404 /404.php

my 404.php:

<html>
<body>
<h1> erro 404</h1>
</body>
</html>

when i envoke the url "http://pr:8080/sao-paulo/12/13" the rewrite rule works perfecly redirecting to sao-paulo.htm...but if i envoke:

[pr:8080...]

i got the default error page...and in the apache error log ive got just the message:

[Tue Sep 29 14:13:11 2009] [error] [client 127.0.0.1] File does not exist: C:/Projetos/PlanetaRestaurante/wwwroot/sao-paulo1.htm

jd01

5:28 pm on Sep 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Let me see if maybe I have an idea.
Try this:

RewriteEngine On

RewriteRule ^404\.php$ - [L]

RewriteCond %{REQUEST_URI} !\.htm$
RewriteRule ^([^.]+)\/([^/]+)\/([^/]+)$ $1.htm [L]

ErrorDocument 404 /404.php

It looks to me like your rule matches anything not ending in .htm, which would include 404.php. I'm not sure what you are doing with the rule exactly, so I'm not sure exactly how to tell you to make it better *, but the rule I added stops your 404.php page from being processed by your RewriteRule... It should work.

* I'm not sure why you need the 3 () when you are only referencing one and the first pattern matches everything up to the . (dot)

Dunno, Maybe?
RewriteRule ^([^.]+)\. $1.htm [L]

[edited by: jd01 at 5:35 pm (utc) on Sep. 29, 2009]

costamatrix

5:34 pm on Sep 29, 2009 (gmt 0)

10+ Year Member



same problem Jim...

just got the default error again..

and in the apache error log the only line is:

[Tue Sep 29 14:32:20 2009] [error] [client 127.0.0.1] File does not exist: C:/Projetos/Planetd/wwwroot/sao-paulo1.htm

costamatrix

5:36 pm on Sep 29, 2009 (gmt 0)

10+ Year Member



now even if i rename my 404.php to 404.htm and altered .htacces to .htm

it does not work anymore... i keep seeing the default error page

jd01

5:37 pm on Sep 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One more try:

ErrorDocument 404 /404.htm

RewriteEngine on

RewriteRule ^404\.htm$ /404.php [L]
RewriteRule ^404.php$ - [L]

RewriteCond %{REQUEST_URI} !\.htm$
RewriteRule ^([^.]+)\/([^/]+)\/([^/]+)$ $1.htm [L]

costamatrix

5:49 pm on Sep 29, 2009 (gmt 0)

10+ Year Member



something new!

with your last try i stil got the same default error page, BUT in the apache error log, got this:

[Tue Sep 29 14:47:48 2009] [error] [client 127.0.0.1] File does not exist: C:/Projetos/Planeta1/wwwroot/sao-paulo.htm

[Tue Sep 29 14:47:48 2009] [error] [client 127.0.0.1] script 'C:/Projetos/Planeta1/wwwroot/404.php' not found or unable to stat

costamatrix

5:53 pm on Sep 29, 2009 (gmt 0)

10+ Year Member



sorry my mistake...i forgot to rename 404.htm back to 404.php

so the log is the same

[Tue Sep 29 14:52:45 2009] [error] [client 127.0.0.1] File does not exist: C:/Projetos/Planeta1/wwwroot/sao-paulo.htm
[Tue Sep 29 14:52:45 2009] [error] [client 127.0.0.1] File does not exist: C:/Projetos/Planeta1/wwwroot/sao-paulo.htm
[Tue Sep 29 14:52:45 2009] [error] [client 127.0.0.1] File does not exist: C:/Projetos/Planeta1/wwwroot/sao-paulo.htm
[Tue Sep 29 14:52:45 2009] [error] [client 127.0.0.1] File does not exist: C:/Projetos/Planeta1/wwwroot/sao-paulo.htm
[Tue Sep 29 14:52:46 2009] [error] [client 127.0.0.1] File does not exist: C:/Projetos/Planeta1/wwwroot/sao-paulo.htm

costamatrix

6:07 pm on Sep 29, 2009 (gmt 0)

10+ Year Member



we can see now (that 404.php) was not in the directory, that apache are looking for it, but is not using it.

costamatrix

6:17 pm on Sep 29, 2009 (gmt 0)

10+ Year Member



i run a test...i remove de mod_rewrite from .htaccess and leave only the line:

ErrorDocument 404 /404.php

even then when i request a page that does not exists, the default error page is displayed....

i believe that is the problem we have to solve first...

jd01

6:42 pm on Sep 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you tried to access /404.php directly to see if you can access it at all?

IOW does http://www.example.com/404.php open when you type it in?

costamatrix

6:52 pm on Sep 29, 2009 (gmt 0)

10+ Year Member



yes, it works normally:

[pr:8080...]

my actual 404.php:

<html>
<body>
<h1> erro 404</h1>
</body>
</html>

Caterham

9:08 pm on Sep 29, 2009 (gmt 0)

10+ Year Member



the default error page is displayed

With an additional note about another error? Something like

Additionally, a ... error was encountered while trying to use an Error Document to handle the request.

costamatrix

9:39 pm on Sep 29, 2009 (gmt 0)

10+ Year Member



no nothing more...its very strange

costamatrix

11:12 pm on Sep 29, 2009 (gmt 0)

10+ Year Member



oh my god!

i sniffed my connection with fiddler....it is working!

my google toolbar was hiding the custom error page!

Man!

Thanks

costamatrix

11:26 pm on Sep 29, 2009 (gmt 0)

10+ Year Member



Problem solved Jim.

Thanks a lot!

jdMorgan

11:36 pm on Sep 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Argh!

Do be sure that your error page is at least 512 bytes long... Otherwise, IE will hide it and shows its own "Friendly error page"... Because of course, MS knows best. See [webmasterworld.com...]

I didn't even think of that problem because I don't use IE for anything except testing the non-standard CSS hacks that it requires...

Jim

costamatrix

12:31 am on Sep 30, 2009 (gmt 0)

10+ Year Member



yes Jim but i was using firefox....it was google toolbar that did this "trick" and show this "friendly error page"...

but its ok

jdMorgan

12:33 am on Sep 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Man, where is that button? -- I need to make sure it's off!

Another example of trying to 'think for the user' and messing things up even more...

Jim

jdMorgan

12:37 am on Sep 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, found it... Toolbar->Options->Search "Provide suggestions for incorrect or unavailable URLs"

Provides Google search suggestions unless the error page is at least 512 bytes in length.

I've never experimented with that one... But another thing to look out for, here.

Jim

costamatrix

12:39 am on Sep 30, 2009 (gmt 0)

10+ Year Member



options -> "Browsing tab" -> make a suggestion for wrong or unavailable URLīs...

jd01

3:26 am on Sep 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Another example of trying to 'think for the user' and messing things up even more...

Yeah, I thought when I quit optimizing for them the headaches stopped, but I would not have guessed the answer was the toolbar in a week... I even know about the IE page size (was just reading about it in fact) and it didn't even cross my mind in this situation.

Somethin' new every day, and not always for the better...