拒绝 htaccess 对文件夹和文件的直接访问

发布于 2025-01-05 18:07:42 字数 427 浏览 1 评论 0原文

场景如下:

  • 根文件夹中有一个 index.php 文件,
  • 一些文件包含在 index.php 中,这些文件位于 includes 文件夹中。
  • 另外 1 个文件 (submit.php) 位于用于表单提交操作的根文件夹中。

我想限制用户通过 htaccess 对 includes 文件夹中的文件进行直接访问。也适用于 submit.php。但 include 将适用于 index.php 文件。 例如,如果用户输入 www.domain.com/includes/somepage.php,它将对其进行限制(可能会重定向到错误页面)。

Here is the scenario:

  • There is a index.php file in root folder
  • some files are included in index.php which are in the includes folder.
  • 1 other file (submit.php) is in the root folder for form submit action.

I want to restrict direct user access to the files in includes folder by htaccess. also for submit.php. But include will work for index.php file.
Like, if user types www.domain.com/includes/somepage.php, it will restrict it (may be redirect to a error page).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

纸短情长 2025-01-12 18:07:42

我只是将 includes 文件夹移出 Web 根目录,但如果您想阻止直接访问整个 includes 文件夹,您可以将 .该文件夹中的 htaccess 文件仅包含:

deny from all

这样您就无法从该文件夹中打开任何文件,但您可以将它们包含在 php 中而不会出现任何问题。

I would just move the includes folder out of the web-root, but if you want to block direct access to the whole includes folder, you can put a .htaccess file in that folder that contains just:

deny from all

That way you cannot open any file from that folder, but you can include them in php without any problems.

缺⑴份安定 2025-01-12 18:07:42

这是纯粹基于 mod_rewrite 的解决方案:

RewriteRule ^(includes/|submit\.php) - [F,L,NC]

如果 URI 包含 /includes//submit.php,这将显示禁止使用的错误

This is pure mod_rewrite based solution:

RewriteRule ^(includes/|submit\.php) - [F,L,NC]

This will show forbidden error to use if URI contains either /includes/ or /submit.php

内心旳酸楚 2025-01-12 18:07:42

可以使用 Files 指令并禁止访问所有文件,然后再次使用它来设置可访问的文件:

<Files ~ "^.*">
  Deny from all
</Files>

<Files ~ "^index\.php|css|js|.*\.png|.*\.jpg|.*\.gif">
  Allow from all
</Files>

It's possible to use a Files directive and disallow access to all files, then use it again to set the files that are accessible:

<Files ~ "^.*">
  Deny from all
</Files>

<Files ~ "^index\.php|css|js|.*\.png|.*\.jpg|.*\.gif">
  Allow from all
</Files>
何以畏孤独 2025-01-12 18:07:42

1 基于 mod_alias 的衬垫解决方案:

RedirectMatch 403 ^/folder/file.php$

这将显示 /folder/file.php 的禁止错误

1 liner mod_alias based solution :

RedirectMatch 403 ^/folder/file.php$

This will show forbidden error for /folder/file.php

场罚期间 2025-01-12 18:07:42

如果我理解正确的话,您只是想拒绝访问包含文件夹?

在包含文件夹中放置带有“DENY FROM ALL”指令的 .htaccess 即可解决问题。

If I understand correctly you just want to deny access to the includes folder?

An .htaccess with a 'DENY FROM ALL' directive placed in the includes folder would do the trick.

药祭#氼 2025-01-12 18:07:42

根据在更高级别设置的可能的其他选项,您可能需要将以下内容放入包含目录中的 .htaccess 文件中:

Satisfy all
Order deny,allow
Deny from all

当上层目录定义基本身份验证时,我遇到了这个问题,包括行:

Satisfy any

这阻止了我的拒绝由于用户已通过身份验证,因此生效。

Depending on possible other options set at a higher level you may need to put the following in your .htaccess file in your includes directory:

Satisfy all
Order deny,allow
Deny from all

I ran into this when the upper directory defined basic authentication including the line:

Satisfy any

This was preventing my deny from all to take effect because the users were authenticated.

深爱成瘾 2025-01-12 18:07:42

你的 Q 分为两部分,jeroen 和 anubhava 的解决方案都适用于第一部分——拒绝访问 /includes。 anubhava 的作品也适用于第二部分。我更喜欢后者,因为无论如何我都使用 DOCROOT/.htaccess 并且这将所有此类控制保留在一个文件中。

然而我想讨论的是“拒绝访问submit.php”的概念。如果您不想使用 submit.php 那为什么要把它放在 DOCROOT 中呢?我怀疑这里的答案是您在某些表单中将其用作操作目标,并且只希望在提交表单时而不是直接(例如从垃圾邮件机器人)提交表单时触发它。

如果这是真的,那么你就不能使用 anubhava 的第二部分,因为这会导致你的表格失败。
您在这里可以做的是 (i) 使用 .htaccess 检查以确保引荐来源网址是您自己的索引页面:

RewriteCond %{HTTP_REFERRER} !=HTTP://www.domain.com/index.php   [NC]
RewriteRule ^submit\.php$    -                                   [F]

以及 (ii) 在 PHP index.php 表单生成器中包含一些隐藏字段时间戳和验证。例如,验证可以是时间戳 MD5 的前 10 个字符和一些内部秘密。在处理提交时,您可以 (i) 验证时间戳和验证是否匹配,以及 (ii) 时间戳在当前时间的 15 分钟内。

这样您就可以防止垃圾邮件发送,因为垃圾邮件发送者获得有效时间戳/验证对的唯一实用方法是解析表单,但这种抓取的寿命只有 15 分钟。

Your Q comes in two parts, both jeroen and anubhava's solutions work for part I -- denying access to /includes. anubhava's also works for part II. I prefer the latter because I use a DOCROOT/.htaccess anyway and this keeps all such control in one file.

However what I wanted t discuss is the concept of "denying access to submit.php". If you don't want to use submit.php then why have it in DOCROOT at all? I suspect that the answer here is that you use it as a action target in some forms and only want it to be fired when the form is submitted and not directly , e.g. from a spambot.

If this is true then you can't use anubhava's part II as this will cause your form to fail.
What you can do here is (i) with the .htaccess check to ensure that the referrer was your own index page:

RewriteCond %{HTTP_REFERRER} !=HTTP://www.domain.com/index.php   [NC]
RewriteRule ^submit\.php$    -                                   [F]

And (ii) within your PHP index.php form generator include some hidden fields for a timestamp and validation. The validation could be, say, the first 10 chars of an MD5 of the timestamp and some internal secret. On processing the submit you can then (i) validate that the timestamp and validation match, and (ii) the timestamp is within, say, 15 minutes of the current time.

This you can prevent spamming as the only practical way that a spammer could get a valid timestamp / validation pair would be to parse a form, but this scrape would only have a 15 minute life.

不忘初心 2025-01-12 18:07:42

您可以将以下命令添加到 .htaccess 文件中,

Deny from all
ErrorDocument 403 "nothing is here"

以防未经授权的访问,它将显示“nothing is here”消息。

如果您想通过错误代码重定向到某个页面,那么您可以定义一个命令,如下所示:

ErrorDocument 404 "/errors/404.html"

它将重定向到 /errors/404.html 并显示自定义页面未找到屏幕。

You can add the below command to .htaccess file

Deny from all
ErrorDocument 403 "nothing is here"

It will display the "nothing is here" message in case of the unauthorised access.

If you want to redirect by an error code to a certain page then you can define a command as follows:

ErrorDocument 404 "/errors/404.html"

It will redirect to the /errors/404.html and show the custom page not found screen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文