Search code examples
.htaccesshttp-redirectfront-controller

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


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!


Solution

  • 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!