Reputation: 91
Look, the rule is:
RewriteCond %{REQUEST_FILENAME} !-f
Whe I put this rule in the .htaccess I'm telling to the webserver that: IGNORE ALL THE FILE PATHS IN THE URL.
For example: http://www.foo.com/images/file.jpg
This file is ignored, the image will only showed, if this URL is processed by a script (.php) and returns the content of the file. I need to do this, but not with all the files:
For example: .JS files NOT and other file types.
How to "REWRITE" that rule to ignore specific type of files or to apply only for specific files like:
ONLY ACCEPTS: JPG, PNG, JPEG, MP3
RewriteCond %{REQUEST_FILENAME}.(jpg|png|jpeg|mp3 ... ) !-f
OR
NO ACCEPTS: JS, TXT, CSS
RewriteCond !%{REQUEST_FILENAME}.(js|txt|css| ... ) !-f
Upvotes: 0
Views: 70
Reputation: 16214
IGNORE ALL THE FILE PATHS IN THE URL.
You are telling to check the existence of the requested file. !-f means "file does not exist at the physical location where the request URL is pointing". So, if the file actually exists at requested location, you php script will not be executed. That is not clear why do you want to add some exclusion, because if the file exists, it will be requested anyway and if it does not exists, your script should generate page with 404 error, otherwise it will be the page generated by the server. Probably not so fancy :)
In addition to the first answer, it should be, probably,
RewriteCond %{REQUEST_FILENAME} !\.(jpg|png|jpeg|mp3)$ [NC]
in order to get rid of case sensitivity.
Combining together
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !\.(jpg|png|jpeg|mp3)$ [NC]
# your RewriteRule goes here
Upvotes: 1
Reputation: 254926
Will match if the url ends with one of listed extensions:
RewriteCond %{REQUEST_FILENAME} \.(jpg|png|jpeg|mp3)$
Will match if the url doesn't end with one of listed extensions:
RewriteCond %{REQUEST_FILENAME} !\.(jpg|png|jpeg|mp3)$
The conditions by default are connected by "AND". This will match only if both A and B match:
RewriteCond A
RewriteCond B
This will match if A or B match:
RewriteCond A [OR]
RewriteCond B
More details: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond
Upvotes: 0