0

How do I say, "if the file is in the /wp-content/* directory, serve it as long as it's .+\.(jpe?g|gif|png|js|css)$, otherwise throw a [F]orbidden."

I can't seem to wrap my head around where to begin.

I'm okay with regex, but my mod-rewrite skills are (obviously) lacking.

Jeff
  • 1,416
  • 3
  • 28
  • 50
  • Both answers by serverninja and Born To Ride are correct, but note that the code should be `%{REQUEST_URI}`, not `{%REQUEST_URI}`. – Jeff Nov 05 '09 at 15:08

2 Answers2

3

A more compact version of serverninja's snippet would be the following:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond {%REQUEST_URI} !^/wp-content/.*\.(jpe?g|gif|png|css|js)
  RewriteRule ^.*$ - [F]
</IfModule>
Born To Ride
  • 1,084
  • 6
  • 10
2

Something like this:

RewriteEngine On
RewriteOptions Inherit

RewriteCond %{REQUEST_URI} ^/wp-content/.*
RewriteCond %{REQUEST_URI} !.+\.(jpe?g|gif|png|css|js)$
RewriteRule ^/(.*)$ - [F]

I'm not at a machine where I can test it, but the idea here is two conditions that check for the wp-content directory in the URI, and then a negated condition for the files you want to serve statically. Everything else should put out a 403 error. If it's not right, it will certainly point you in the right direction ;-)

edit: syntax should be %{} not {%} -- thanks Jeff!

Sam Halicke
  • 6,222
  • 1
  • 25
  • 35