【问题标题】:How to put 403 page when user try to access a file directly?(.htaccess)当用户尝试直接访问文件时如何放置403页面?(.htaccess)
【发布时间】:2015-04-01 06:33:08
【问题描述】:

我正在使用 apache 服务器,请考虑这是我的网站:www.domain.com。

我将所有文件、图像、视频保存在子域中,例如-> http://sub.domain.com/file.jpg

我正在访问子域中的所有文件(即图像、视频)。

如果有用户直接在地址栏(http://sub.domain.com/file.jpg)绑定访问我的文件,它应该显示 403 禁止页面。

如果有任何用户想要访问我的网站(www.domian.com),它应该允许访问。

我尝试了以下 .htaccess 代码,

Options -Indexes
RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?sub.domain.com [NC] 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?sub.domain.com.*$ [NC] 
RewriteRule \.(gif|jpg|png|avi|mp3|swf")$ - [F]

但问题是,当我访问我的网站时,它也会给出 403 禁止结果。

更新:

index.html in (domain.com)

 <html>
 <body>
   <h1>Example Page</h1>
   <img src="http://sub.domain.com/file.jpg">
 </body>
 </html>

输出:(当我尝试访问“domain.com”时)

Example Page

图片未加载。它在“控制台”中显示 403 禁止。

【问题讨论】:

    标签: apache .htaccess mod-rewrite http-status-code-403


    【解决方案1】:

    http:// 不会出现在引荐来源网址中,只会出现在域名中。请尝试以下方法:

    RewriteEngine On 
    RewriteCond %{HTTP_REFERER} !^(www\.)?sub\.domain\.com [NC] 
    RewriteRule \.(gif|jpg|png|avi|mp3|swf)$ - [F]
    

    【讨论】:

    • 您在哪个 URL 上进行测试?整个页面没有加载吗?你还有其他规定吗?
    • 这意味着规则有效。您没有sub.domain.com 的推荐人,因此禁止显示。尝试使用 curl 或 REST 控制台进行测试并手动设置引荐来源网址,看看会发生什么。
    猜你喜欢
    • 2013-11-27
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 2021-01-11
    相关资源
    最近更新 更多