【问题标题】:Prevent direct access to .htaccess file防止直接访问 .htaccess 文件
【发布时间】:2014-02-07 07:45:25
【问题描述】:

我需要阻止访问.htaccess 文件。我的文件是:

# secure htaccess file
<Files .htaccess>
 order allow,deny
 deny from all
</Files>

# disable directory browsing
Options All -Indexes

当我检查网址时:

http://localhost/cms/htaccess

我看到.htaccess文件数据

我如何prevent/deny 访问.htaccess 文件?

【问题讨论】:

    标签: regex apache .htaccess


    【解决方案1】:

    您正在阻止.htaccess,但正在访问http://localhost/cms/htaccess

    尝试访问http://localhost/cms/.htaccess

    如果你想同时阻止这两个,试试这个FilesMatch 正则表达式指令:

    <FilesMatch "\.?htaccess$">
     order allow,deny
     deny from all
    </FilesMatch>
    

    【讨论】:

    • 是的,我也无法访问http://localhost/cms/.htaccess 但您的代码不起作用,我在http://localhost/cms/htaccess 中看到了htaccess 文件。
    • 我已经测试过了,它工作正常。你把它放在哪个 .htaccess 中?
    • 哦!这行得通。我对没有dot.htaccesshtaccess 文件的问题!
    • 是的,这就是我在关于 DOT 的回答中所写的内容:) 很高兴知道它有效。
    【解决方案2】:

    您可以在 htaccess 文件本身内保护访问, 该指令的范围仅适用于该目录, 请参阅下面的代码。

    保护 htaccess 文件。

    <Files "log.txt">
    Order Allow,Deny
    Deny from all
    </Files>
    

    在您本地的 htaccess 文件中。您也可以使用 mod_rewrite 来处理拒绝和访问 htaccess 文件以及 log.txt 的情况:

    RewriteRule /?\.htaccess$ - [F,L]
    
    RewriteRule ^/?inscription/log\.txt$ - [F,L]
    

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 2019-09-20
      • 2011-07-23
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多