Forum Moderators: phranque

Message Too Old, No Replies

.htaccess RewriteEngine - 404 errors

I'm having some issues with a .htaccess file, RewriteEngine and an Internal

         

1square

5:39 pm on Apr 19, 2010 (gmt 0)

10+ Year Member



I'm having some issues with a .htaccess file, RewriteEngine and an Internal Server Error. Everything on this site works fine and the url rewrite is working perfectly. However if I try to access a page that doesn't exist, instead of a 404 I'm getting an internal server error. I have included my .htaccess file below.

# removes all .php extensions
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /$1.php [L,QSA]

# forces download of file type
AddType application/octet-stream .ies

# Error doc.
ErrorDocument 404 /search.php?var=404


Has anyone encountered this before?

1square

6:56 pm on Apr 19, 2010 (gmt 0)

10+ Year Member



Found the issue when reviewing my error logs. Looks as though an infinite loop was being created. I've added the code snippet necessary to prevent this below.

# removes all .php extensions
# ONLY works for www.site.com/test
# NOT www.site.com/test/

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# prevent infinite loop
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

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

# forces download of file type
AddType application/octet-stream .ies

# Error doc.
ErrorDocument 404 /search.php?var=404

jdMorgan

8:34 pm on Apr 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Another solution is to verify that <xyz>.php exists before you try to rewrite to it.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteRule ^(.+)$ /$1.php [L,QSA]

Note that you really should make an effort to exclude as many non-page objects from this rule as possible because all of these file-exists checks are *very* expensive in terms of server performance. By adding a RewriteCond ahead of all the others to exclude image, css, .js, and document files, for example, you may see a noticeable improvement in server response under load.

Example:
 RewriteCond $1 !\.(gif|jpe?g|png|ico|css|js|pdf|mp3|flv|swf)$ 


This prevents the file-exists checks from being done when not required. Adjust filetypes to suit.

Jim

g1smd

10:41 pm on Apr 19, 2010 (gmt 0)

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



That additional line can make a noticeable difference.

It is important!