Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite rewriting error pages

         

FutureMillennium

1:07 pm on Jul 11, 2010 (gmt 0)

10+ Year Member



Hello,

So basically, my .htaccess is like this:

ErrorDocument 404 /404.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]


What this does is that it turns "/anything" into "/anything.php" when "/anything" is not an existing file or directly. This is fine and works as it should.

The problem is when you go to "/anything" and "/anything.php" doesn't exist. This throws a 500 Internal Server Error. My guess is that what's happening is:

1) mod_rewrite turns /anything into /anything.php
2) ErrorDocument tries to redirect it to 404.php, but under the same displayed URI
3) mod_rewrite again tries to display /anything.php instead
4) etc., infinited loop

I might be wrong, but the question is:
How do I stop this from happening?

Thank you

g1smd

3:42 pm on Jul 11, 2010 (gmt 0)

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



Make your pattern not applicable to "anything"; narrow it's scope somewhat.

You can then also likely do away with the slow -f and -d "exists" checks too.

FutureMillennium

4:21 pm on Jul 11, 2010 (gmt 0)

10+ Year Member



But I don't want to do that.

I want to know why it's not working for error pages and how to avoid it.

jdMorgan

9:00 pm on Jul 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest:

RewriteCond $1 !\.(gif|jpe?g|png|ico|css|js|xml|mp[34]|mpe?g|wmv|avi|mov|swf|flv|pdf|doc|txt)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ /$1.php [L]

Because this adds yet another filesystem check (i.e another potential disk read), I took the liberty of adding the first RewriteCond in an attempt to avoid doing up to three disk checks for every single request to your server... as g1smd suggested above.

It is not critical to include *all* filetypes which are not generated by your script in this exclusion list, it is only important to include the most-frequently-requested filetypes not generated by your script. After four dozen filetypes or so, you may reach a point of diminishing returns. Adjust to suit.

The leading slash on the RewriteRule substitution path is recommended for security reasons, so that the user-agent cannot control the root of the requested filepath.

Jim