Forum Moderators: phranque

Message Too Old, No Replies

.htaccess rewrite if files are not existing, exepted default website

rewrite if files are not existing, exepted default website

         

marc waesche

2:43 pm on Aug 24, 2010 (gmt 0)

10+ Year Member



Hello!

I want that my server makes a rewrite to the file 'handler.php' if a not existing file is called, i.e. [domain.com...]
With the .htaccess code below it works generally but if the user calls the domain without the default file index.php the user is also rewrited because the apache obviously thinks this file name (without content) is also not existing.
So, how must the code look like that it knows the domain entered without a file name is an exeption?

Best regards
Marc


rewriteEngine on
rewriteBase /
rewriteCond %{REQUEST_FILENAME} !-f
rewriteRule ^.* handler.php [L]

jdMorgan

6:12 pm on Aug 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteEngine on
RewriteBase /
#
# Internally rewrite all non-blank URL-path requests which do not resolve to physically-
# existing files to handler.php unless they have already been rewritten to handler.php
RewriteRule $1 !^handler\.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ handler.php [L]

Note that handler.php now becomes responsible for generating all directories, images, multimedia files, css files, and external Javascripts which do not exist in physical files. It is also responsible for generating correct 404 and 410-Gone responses if it cannot generate them.

If your handler script cannot generate images, css files, Javasscripts, etc., then you should also exclude these filetypes from being rewritten to that script, using an additional RewriteCond such as:

RewriteCond $1 !\.(gif|jpe?g|png|ico|css|js)$

As an added bonus, the more of these filetypes you exclude, the faster your server will run, because it will not have to go read the disk to see if any of these filetypes exist or not... That is a very slow and resource-intensive function, so the less often you do it, the better. Plus your hard drive will last longer... :)

Consider also excluding "well-known" files such as robots.txt sitemap.xml, labels.rdf, and /w3c/p3p.xml

Jim

marc waesche

7:24 pm on Aug 24, 2010 (gmt 0)

10+ Year Member



Thank you very much for your professional help, Jim!

Marc