【问题标题】:Apache: how to block access to files like Gruntfile.js, package.json, etc?Apache:如何阻止对 Gruntfile.js、package.json 等文件的访问?
【发布时间】:2016-05-13 17:29:03
【问题描述】:

我有 Gruntfile.js 和 package.json 以及文件夹 node_modules 和 .git。 我想阻止对这些文件和文件夹的访问。在应用程序中,我将 index.php 作为每个非文件/文件夹请求的入口

这是在我的 .htaccess 中

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ index.php?page=$1

在 index.php 中

if (isset($_GET['page'] && !file_exists($views.$_GET['page'])) {
    header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
    include $views.'404.php';
}

所以如果这些私有文件在各自的视图中显示为 404 会更好

【问题讨论】:

  • 您要阻止node_modules 目录 中的所有内容,还是仅阻止对该目录的root 访问?
  • @MikeRockett 一切

标签: apache .htaccess http-status-code-404


【解决方案1】:

我的解决方案是使用简单的 RewriteRule 将 index.php 中的每个私有文件和文件夹重定向到 404 视图

RewriteEngine On
RewriteRule ^Gruntfile\.js$ index.php?page=404 [L]
RewriteRule ^package\.json$ index.php?page=404 [L]
RewriteRule ^.git/.*$ index.php?page=404 [L]
RewriteRule ^node_modules/.*$ index.php?page=404 [L]

虽然我认为这类文件需要在根文件夹中,并且 index.php 在像public_html 这样的子文件夹中,这是唯一的公共

此解决方案甚至适用于大写字母和混合字母。即:

pacKagE.json
gruntfile.js
.GIT/

【讨论】:

  • 这确实将请求从浏览器重定向到文件夹,但它也将包含请求重定向到 <script><link>
猜你喜欢
  • 1970-01-01
  • 2011-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-14
  • 1970-01-01
  • 1970-01-01
  • 2014-03-02
相关资源
最近更新 更多