Php symfony2.htaccess web文件夹阻止对某些子文件夹的访问

Php symfony2.htaccess web文件夹阻止对某些子文件夹的访问,php,.htaccess,symfony,assets,Php,.htaccess,Symfony,Assets,我在web文件夹中有以下子文件夹 /主题/shop/1/html /主题/店铺/1/资产 /主题/shop/2/html /主题/店铺/2/资产 我想阻止访问主题文件夹中的任何html文件夹,只允许访问所有资产文件夹 如何使用.htaccess或symfony2访问控制来实现这一点?如果用户试图访问,可以通过向用户提供HTTP 403禁止错误来使用.htaccess: RewriteRule ^/themes/shop/(.*)/html(.*) - [L,R=403] 或者,如果您更喜欢

我在web文件夹中有以下子文件夹

  • /主题/shop/1/html
  • /主题/店铺/1/资产
  • /主题/shop/2/html
  • /主题/店铺/2/资产
我想阻止访问主题文件夹中的任何html文件夹,只允许访问所有资产文件夹


如何使用.htaccess或symfony2访问控制来实现这一点?

如果用户试图访问,可以通过向用户提供HTTP 403禁止错误来使用.htaccess:

RewriteRule ^/themes/shop/(.*)/html(.*) - [L,R=403]
或者,如果您更喜欢404-未找到:

RewriteRule ^/themes/shop/(.*)/html(.*) - [L,R=404]
既然您提到您正在使用Symfony,那么您还应该注意将规则放置在正确的位置,以便其他重写规则不会接管:

<IfModule mod_rewrite.c>
    RewriteEngine On

    #Put them first, otherwise they might get routed elsewhere
    RewriteRule ^/themes/shop/(.*)/html(.*) - [L,R=403]

    #...

</IfModule>

我已经设法用RequestCond处理了您的代码片段,并提供了一些额外的保护。如果你在回答中加上以下内容,我将接受@Basit,很高兴听到代码片段的帮助-我已经更新了我的答案
# contents of the gist linked above - in case the link ever breaks    

# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} !^/themes/stores/(.*)
RewriteCond %{REQUEST_URI} !^/themes/shop/(.*)
RewriteRule .? - [L]

# allow stores asset folder only
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|svg|svgz|eot|otf|woff|woff2|ttf|swf|php|ico|txt|pdf|xml)$
RewriteCond %{REQUEST_URI} ^/themes/stores/(.*)/asset/.*
RewriteRule .? - [L]

# allow shop asset folder only
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|svg|svgz|eot|otf|woff|woff2|ttf|swf|php|ico|txt|pdf|xml)$
RewriteCond %{REQUEST_URI} ^/themes/shop/(.*)/asset/.*
RewriteRule .? - [L]