.htaccess - 如何将这个异常添加到htaccess中?

标签 .htaccess security iframe

我使用名为 Articulate Storyline 的软件创建了这门类(class),您可以从那里导出为 html5。我网站上的 LMS 插件管理对类(class)的访问,并且 html5 导出作为 iframe 包含在那里。为了防止从 LMS 外部访问 html5 导出,我在带有 html5 导出的服务器文件夹中添加了一个 htaccess 文件,符合 this post .

导出的类(class)文件位于:/public_html/courses/course_a/(有多个类(class))。
类(class)网址:https://example.com/coursename/courseA/(其中有一个指向 https://example.com/courses/course_a/story.html).

我创建了/public_html/courses/.htaccess:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !\example\.com/coursename/ [NC]
RewriteRule ^ - [F,L]

一切正常,只是如果您加载类(class),控制台中会出现以下 403 错误:

GET https://example.com/courses/course_a/html5/lib/stylesheets/mobile-fonts/open-sans-light.woff net::ERR_ABORTED 403 desktop.min.css:1 
GET https://example.com/courses/course_a/html5/lib/stylesheets/mobile-fonts/open-sans-regular.woff net::ERR_ABORTED 403 desktop.min.css:1 
GET https://example.com/courses/course_a/html5/lib/stylesheets/mobile-fonts/open-sans-bold.woff net::ERR_ABORTED 403 desktop.min.css:1

我认为我添加的 .htaccess 文件会导致此问题。有没有办法在 .htaccess 文件中为 /mobile-fonts/ 文件夹或 woff 文件创建异常?


更新:我将/public_html/courses/.htaccess更新为:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https://example\.com/coursename/ [OR]
RewriteCond %{HTTP_REFERER} !^https://example\.com/courses/
RewriteRule ^ - [F,END]

但是,我现在收到 https://example.com/coursename/courseA/(iframe 为 403)和 https://example.com 的 403 错误/courses/course_a/story.html。对于第二个,我确实想要 403,但对于第一个则不需要。

如果我删除[OR] RewriteCond %{HTTP_REFERER} !^https://example\.com/courses/https://example.com/coursename/courseA/ 有效并且不会返回 403。但是我什至可以将第一个 RewriteCond 更改为 !^https://example\.com/coursename/fhwfhf 并且它仍然加载 iframe。 ..

最佳答案

正如问题评论中所讨论的,一种方法是将加载到该 iframe 本身的“页面”的 URL 添加到您的条件:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https://example\.com/url-that-includes-the-iframe
RewriteCond %{HTTP_REFERER} !^https://example\.com/folder-that-iframe-points-to
RewriteRule ^ - [F,END]

直接输入的指向 https://example\.com/folder-that-iframe-points-to 的 URL 根本不应该设置引用站点,所以我希望这样的请求被屏蔽了...


另一种方法是明确允许访问由传递到 iframe 的“页面”引用的资源。这可以通过在实际阻止规则之前添加这样的异常来完成:

RewriteEngine On
RewriteRule ^/?html5/lib/stylesheets/ - [END]
RewriteCond %{HTTP_REFERER} !^https://example\.com/url-that-includes-the-iframe
RewriteRule ^ - [F,END]

或者,也可以通过将该异常(exception)添加到实际阻止规则的条件中来实现相同的目的。请注意,此变体指定[OR]操作数,需要应用默认的逻辑“and”运算符:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https://example\.com/url-that-includes-the-iframe
RewriteCond %{REQUEST_URI} !^/html5/lib/stylesheets/
RewriteRule ^ - [F,END]

对于所有这些方法,重写模块都需要加载并激活到 http 服务器中。这些规则最好放入 http 服务器内的实际主机配置中。如果无法访问该文件,则可以使用分布式配置文件(“.htaccess”),但这会带来一些缺点,并且需要启用此类文件的解释(请参阅文档...)。

关于.htaccess - 如何将这个异常添加到htaccess中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66350664/

相关文章:

php - 限制授权 php 用户的文件访问

apache - 为什么 .htaccess 不阻止 apache 中的文件夹?

.htaccess - 用共享 ssl 重写 htaccess

android - 在 Android 和 iOS 上保护 HTTP API 调用的最简单和最安全的方法是什么?

windows - WCF 异常 : . ..此服务需要 'Anonymous' 身份验证,但未为托管此服务的 IIS 应用程序启用

jquery - iframe fancybox 响应式

javascript - 使用addon sdk时如何向firefox扩展添加内容安全策略?

php - SEO 友好 URL 而不是查询字符串 .htaccess

c# - 具有管理权限的进程在用户登录时运行

jquery - 通过api和Jquery事件显示/隐藏模式时启动/停止youtube iframe