【问题标题】:Deny for all php files, but allow specific folder with php files拒绝所有 php 文件,但允许包含 php 文件的特定文件夹
【发布时间】:2019-06-10 18:57:15
【问题描述】:

我想拒绝访问所有子文件夹中的所有 PHP 文件,但允许访问特定子文件夹中的所有 PHP 文件(例如 test)。

我尝试了很多,但找不到任何解决方案。

这是整个 htaccess 文件

<filesmatch \.(php|phtml)$>
    deny from all
</filesmatch>
<filesmatch (index.php|install.php)>
    allow from all
</filesMatch>
<ifmodule mod_deflate.c>
    <filesmatch \.(css|js|php|phtml|svg|woff|xml)$>
        setoutputfilter deflate
    </filesmatch>
</ifmodule>
<ifmodule mod_security.c>
    secfilterengine off
    secfilterscanpost off
</ifmodule>
<ifmodule mod_rewrite.c>
    rewriteengine on
    rewritecond %{https} off
    rewritecond %{http_host} ^www\.(.*)$ [nc]
    rewriterule ^(.*)$ http://%1/$1 [r=301,l]
    rewritecond %{https} on
    rewritecond %{http_host} ^www\.(.*)$ [nc]
    rewriterule ^(.*)$ https://%1/$1 [r=301,l]
    rewritecond %{request_filename} !-d
    rewriterule ^(.*)/$ /$1 [r=301,l]
    rewritecond %{request_filename} -f
    rewriterule ^(.*) $1 [l]
    rewritecond %{request_filename} !-d
    rewriterule ^([^.]*)$ ?p=$1 [l]
    rewritecond %{request_filename} !-d
    rewriterule ^([^.]*).([\w]{2})$ ?p=$1&l=$2 [l]
    rewritecond %{request_filename} !-d
    rewriterule ^([^.]*).([\w]{3,})$ ?p=$1&t=$2 [l]
</ifmodule>
<ifmodule mod_headers.c>
    <filesmatch \.(gif|ico|jpg|svg|png|woff)$>
        header set cache-control max-age=2419200
    </filesmatch>
    <filesmatch \.(css|js|swf)$>
        header set cache-control max-age=604800
    </filesmatch>
    <filesmatch \.(phtml|xml)$>
        header set cache-control max-age=600
    </filesmatch>
</ifmodule>
fileetag none

谢谢!

【问题讨论】:

  • 你有一个.htaccess 文件允许 PHP 在测试文件夹中吗?
  • @Dave 不,但我可以添加这个文件

标签: php .htaccess


【解决方案1】:

在我编写的系统中,我在代码中添加了以下行,我只希望我希望他们访问的人可以访问这些代码:

defined('AUTH') or die(header('HTTP/1.0 403 Forbidden'));

对于那些 .php 文件,如果我希望他们使用/访问那个特定的 PHP 脚本,我会在 include()require() 文件之前将此行添加到顶部:

define('AUTH', true);

这样,对那些没有定义“AUTH”的文件的任何访问尝试都会得到 HTTP 403。这也禁止直接链接访问。 ;) 也许这个解决方案也适合你。

【讨论】:

    猜你喜欢
    • 2016-03-23
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 2016-04-18
    • 2020-11-15
    • 2013-11-27
    • 2021-08-13
    • 2012-09-02
    相关资源
    最近更新 更多