Jump to content

Help with regular expression in .htaccess file


127.0.0.1

Recommended Posts

Using an .htaccess file I want to block all access (except from localhost) of files with one exception. The exception being image files ending with [b].thumb.*[/b] in the file name. The asterisk denoting jpg|jpeg|gif|png extensions.

Examples:
Allow access to [green]image4849.thumb.jpg[/green]
Deny access to [red]image4948.jpg[/red] and any other file [u]not[/u] containing ".thumb.*"

This is what I've come up with, it's a mess and obviously doesn't work... :-\

[code]
<FilesMatch "(.thumb.(gif|jpg|jpeg|png)$)">
    Order Allow, Deny
    Deny from all
    Allow from localhost
</FilesMatch>
[/code]
Link to comment
Share on other sites

Well with all due respect, I've read many sites like that including the documentation at apache.org, and that's why I've come here to get explicit help rather than a referral to another site.

Maybe my question is more of a regexp one and less an htaccess question? In that, I want my regexp to match everything BUT (*.thumb.jp?g|gif|png). I don't know how to tell FilesMatch to do that.
Link to comment
Share on other sites

Yeah, I was just looking at the error logs, "[tt]order takes one argument, 'allow,deny', 'deny,allow', or 'mutual-failure'[/tt]"

I sort of have it working now:

[code]
<FilesMatch "\.thumb\.(gif|jpe?g|png)$">
order allow,deny
deny from all
</FilesMatch>
[/code]

Except, in reverse. :D

It's allowing access to everything but [tt]thumb.gif|jp?g|png[/tt] files. I want it to do the INVERSE; [u]allow[/u] access to [u]only[/u] [tt]thumb.gif|jp?g|png[/tt] files. That's what I've been struggling with all along.
Link to comment
Share on other sites

I dug up an old PHP book and skimmed through some pages on regular expressions.

I got it to work using:

[code]
<FilesMatch "[^\.thumb]\.(gif|jpe?g|png)$">
order allow,deny
deny from all
</FilesMatch>
[/code]

I'm no expert, so if anyone sees an error in that, please let me know, and thanks for the help.
Link to comment
Share on other sites

[tt][ ][/tt]'s denote a character class, meaning any one of the characters inside. The ^ changes the behavior from include to exclude. If Apache follows PCRE as it states, the expression will work for your case, but it will also allow a.gif, c.gif, etc.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.