2012-02-28 88 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