symfony2 .htaccess Web文件夹阻止访问某些子文件夹

简单的

我在Web文件夹中有以下子文件夹。

  • / themes / shop / 1 / html
  • / themes / shop / 1 / asset
  • / themes / shop / 2 / html
  • / themes / shop / 2 / asset

我想阻止访问主题文件夹内的任何html文件夹,并仅允许访问所有资产文件夹。

如何使用.htaccess或symfony2访问控制来实现此目的?

HPierce

您可以使用.htaccess来实现此目的,方法是为用户提供HTTP 403-禁止访问错误(如果他们尝试访问它们):

RewriteRule ^/themes/shop/(.*)/html(.*) - [L,R=403]

或者,如果您更喜欢404-Not Found:

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>

根据@basit链接的注释和要点,您可以添加其他检查,以确保对resource:的任何请求都存在,实际上是资产文件,并且仅包含在assets文件夹中。

# 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]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

.htaccess在子文件夹中

子文件夹中的Rewriterule .htaccess

.htaccess使同级文件夹可以从当前文件夹访问

.htaccess规则文件与所有子文件夹匹配(子文件夹是随机创建的)

htaccess子文件夹中只能访问一个文件并保留url

htaccess阻止直接访问文件夹(例外:通过php)

子文件夹wordpress的htaccess冲突

.htaccess:内部重写到子文件夹,拒绝通过子文件夹进行访问

htaccess阻止文件夹中的特定文件

除非删除Web文件夹中的.htacess文件,否则Symfony2无法访问app_dev.php。

.htaccess阻止/ users文件夹中的php

使用带有子文件夹的.htaccess隐藏文件夹

WordPress的子文件夹的URL htaccess的

从htaccess禁用文件夹公共访问

.htaccess文件夹浏览

如何使用htaccess将子文件夹1中的所有项目重定向到子文件夹2而不是子文件夹本身(永久重定向)

.htaccess:如何在URL中删除子文件夹的子文件夹?

使.htaccess文件在子文件夹中工作

限制通过htaccess访问某些文件夹及其子文件夹的最佳方法

htaccess限制对文件夹和子文件夹的访问

htaccess重写子文件夹?

使用htaccess删除子文件夹

htaccess子域,具有子文件夹访问权限

将(htaccess)子文件夹路由到文件

使用 .htaccess 拒绝访问子文件夹和文件

Htaccess 将子文件夹重写为子域

.htaccess 重定向但排除某些文件夹

阻止我现有的文件夹名称并使用自定义文件夹名称进行访问 [htaccess]

如果是特定文件夹,.htaccess 将规则重写到子文件夹