2012-02-28 90 views
0

我有一个网站,我有一个子文件夹那里有文件,我喜欢在我的索引页上嵌入。但我想让用户直接查看该子文件夹。.htaccess:允许iframe从subdir只能从根目录查看

所以。我有http://mysite.com/index.php那里有一个iframe:

和我有一个子文件夹OPT2与index.php文件与一个iframe:

<iframe src="http://mysite.com/opt2/" id="shopIframe" name="shopIframe" onload="alert('a');" frameborder="0" height="90%" width="100%" scrolling="auto"></iframe> 

我有一个.htaccess文件:

RewriteEngine on 
RewriteCond %{HTTP_REFERER} http://mysite.com/opt2/ [NC] 
RewriteRule /*$ http://mysite.com/index.php [R=302,L] 

是.htacces可能吗?

+1

你可能会在serverfault或网站管理员 – 2012-02-28 17:09:17

回答

1

引用者将是http://mysite.com/index.php而不是http://mysite.com/opt2/

另外一些防火墙软件,代理等移除引用头,所以你最好先检查引用是否可用。

RewriteCond %{HTTP_REFERER} !^$ 
RewriteCond %{HTTP_REFERER} !^http://mysite\.com/ [NC] 
RewriteRule ^opt2/ http://mysite.com/index.php [R=302,L] 

此外

<script> 
if(self == top) 
    location.href='/index.php'; 
</script> 
+0

更好的运气,它的工作原理但一个奇怪的行为。 如果我打开mysite.com/opt2/ - 它显示的内容(但它不应该),但如果我按F5我将被重定向到根目录 – 2012-02-28 19:04:43

+0

如果你想强制它框架,你可以添加一些JS要做到这一点。见上面的编辑。您可以使用referer头,因为您不知道referer头是否被阻止,或者不存在,因为url是直接加载的。 – Gerben 2012-02-28 20:23:49

+0

最终可行。谢谢。 – 2012-02-29 19:48:40