How to redirect all requests to a file, except images using Apache?

user893856 picture user893856 · Aug 15, 2011 · Viewed 40.6k times · Source

I have an .htaccess file with the following contents:

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^(.*)$ entryPoint.php [QSA]
</IfModule>

With this, I want all requests to be redirected to the file entryPoint.php so that it can examine them:

  • If there is no extension, is must be a module
  • If there's a .php, it's a hacking
  • If it's a .png, then it's a "safe" call.

    In case of images, I used to output headers, and file_get_contents() their content. I figured out it's a bit slower than leaving a direct read.

My question is:

How to prevent this .htaccess from calling entryPoint.php if there are references for images?

Extra feedback on the code I already have is greatly appreciated!

Answer

undone picture undone · Aug 15, 2011

As I understand you want to process all non-images threw your php file. Right?

If so, then here is what you need:

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteCond %{REQUEST_URI}  !(\.png|\.jpg|\.gif|\.jpeg|\.bmp)$
RewriteRule (.*)  entryPoint.php [QSA]

</IfModule>

Remember to add to delete any image extension you want! If I misunderstood,Please tell me to correct my answer!