Forum Moderators: phranque

Message Too Old, No Replies

FilesMatch Case Sensitive?

If FilesMatch is case sensitive, how can I make it insensitive?

         

MickeyRoush

10:17 pm on Jul 1, 2011 (gmt 0)

10+ Year Member



I've searched quite a few places on the web, without finding a definite solution. If FilesMatch is case sensitive, how can I make it case insensitive?

apache.org gave the example:

<FilesMatch \.(?i:gif|jpe?g|png)$>
Order allow,deny
Deny from all
</FilesMatch>

But using ?i: only gave internal server errors.

I know with php, I could do something like this (using /i):
'/(jpe?g|gif|png)$/i'

I decided to try it with expires headers (this was just a test).

ExpiresActive On
ExpiresDefault A259200
# Set up caching on media files for 1 month
<FilesMatch "\.(ico|gif|jpe?g|png|flv|pdf|swf|mov|mp3|wmv|ppt)$/i">
ExpiresDefault A29030400
Header append Cache-Control "public"
</FilesMatch>

This did not return any errors and YSlow gave the OK for expires headers. But is this correct. What about these two?

<FilesMatch "\.(i:ico|gif|jpe?g|png|flv|pdf|swf|mov|mp3|wmv|ppt)$">

<FilesMatch "\.(ico|gif|jpe?g|png|flv|pdf|swf|mov|mp3|wmv|ppt)$i">

Neither of the two above caused any errors and YSlow gave the OK for expires headers as well. Sorry, I'm not that good with regular expression and any help would be greatly appreciated.

wilderness

12:21 am on Jul 2, 2011 (gmt 0)

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



([Jj][Pp][Gg]|[Pp][Nn][Gg])

lucy24

1:48 am on Jul 2, 2011 (gmt 0)

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



([Jj][Pp][Ee]?[Gg]|[Pp][Nn][Gg])

But Files and FilesMatch refer to your own actual files, not to user requests, so it shouldn't be necessary to deal with cases. Unless, ahem, you've been slapdash in your file naming. Similarly you don't need to include every extension under the sun, unless you really do have files with those extensions.

Anyway, it may be easier and lead to the same result if you do it with a Rewrite.

MickeyRoush

12:29 am on Jul 4, 2011 (gmt 0)

10+ Year Member



@ wilderness
([Jj][Pp][Gg]|[Pp][Nn][Gg])

Thanks, I'm aware of this method and I currently use it. I just thought that it might be easier to use the i flag if you're going to include a long list of file extensions.

@ lucy24
([Jj][Pp][Ee]?[Gg]|[Pp][Nn][Gg])

But Files and FilesMatch refer to your own actual files, not to user requests, so it shouldn't be necessary to deal with cases. Unless, ahem, you've been slapdash in your file naming. Similarly you don't need to include every extension under the sun, unless you really do have files with those extensions.

I was going to try and cover as many extensions that I was currently using and any potential ones that might be used in the future. Also, registered users are allowed to upload certain files that are restricted by size and type. I decided to allow for the chance that the file extension may be in caps, hence the need for case insensitivity. I have php set up for their uploads to allow for case insensitivity using /i flag(s).

Anyway, it may be easier and lead to the same result if you do it with a Rewrite.

Maybe using a Rewrite is the way to go.
Using this for the first part of it:
RewriteCond %{REQUEST_FILENAME} \.(ico|gif|jpe?g|png|flv|pdf|swf|mov|mp3|wmv|ppt)$ [NC]


Anyways, I was experimenting with different ideas with different things I would like to do, and I came across some sites that suggested using the ?i flag(s). That using FilesMatch will allow it. I just couldn't get it to work as they proposed. I was hoping someone here might have experience with using the ?i flag(s). (Note: I couldn't get the Rewrite to do what I wanted, but using FilesMatch worked fine.) Thank you for your suggestions.

g1smd

12:38 am on Jul 4, 2011 (gmt 0)

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



Rather than allow any-case filenames, I would force all lower-case on the filename, or at the very least on the extension, as each file is uploaded.

MickeyRoush

8:04 am on Jul 7, 2011 (gmt 0)

10+ Year Member



@g1smd

Thank you. I'll probably apply your suggestion.

I respect everyone's recommendations here highly. I'll continue to play with this and if I come up with anything worth mentioning, I'll post it here.

Thanks again!