最近,我的整个站点在iframe中被另外两个域调用。我想阻止其他网站,谁是试图显示我的网站在iframe。
我如何通过.htaccess阻止它?
发布于 2011-05-04 17:17:21
我不认为你可以通过.htaccess,但是你可以使用JS。您可以使用类似下面的函数来检查:
function parentIsSameOrigin()
{
var result = true;
if (window.parent)
{
result = Boolean
(
// more precise modifications needed here
window.this.location.href.indexOf(window.parent.location.href) == 0
);
}
return result;
}
发布于 2016-01-26 22:45:35
你不能每次都“强制执行”它,因为有其他的方法,但是你可以使用标准的头方法。html5-boilerplate有一个很好的vhost/htaccess代码片段,它首先将X-Frame-Options
设置为您选择的DENY/SAMEORIGIN/ALLOW-FROM
,然后允许在好的框架中使用白名单MIME类型,比如谷歌图像搜索。
查看最新链接,但这里是2016年1月25日在SAMEORIGIN
模式下的示例:
<IfModule mod_headers.c>
Header set X-Frame-Options "SAMEORIGIN"
<FilesMatch "\.(appcache|atom|bbaw|bmp|crx|css|cur|eot|f4[abpv]|flv|geojson|gif|htc|ico|jpe?g|js|json(ld)?|m4[av]|manifest|map|mp4|oex|og[agv]|opus|otf|pdf|png|rdf|rss|safariextz|svgz?|swf|topojson|tt[cf]|txt|vcard|vcf|vtt|webapp|web[mp]|webmanifest|woff2?|xloc|xml|xpi)$">
Header unset X-Frame-Options
</FilesMatch>
</IfModule>
发布于 2013-11-12 15:27:00
您可以使用.htaccess,如下所示
RewriteEngine On
RewriteCond %{QUERY_STRING} !^id=[^&]+ [NC]
# if referrer is bad.com
RewriteCond %{HTTP_REFERER} (www\.)?bad\.com [NC]
# then redirect to a different page
RewriteRule !^404.shtm [L,NC,R=302]
为此,您需要依赖HTTP_REFERER。此代码将把来自bad.com的所有请求重定向到not found页面
这个解决方案促成了这个答案。
https://stackoverflow.com/questions/5881139
复制相似问题