拒绝使用htaccess直接访问文件

时间:2014-04-01 07:30:23

标签: php .htaccess

我的网站中有以下目录结构,

/public_html
/public_html/admin/
/public_html/admin/js/
/public_html/admin/css/ ....

基本上我想禁止所有直接访问/ admin子文件夹内的文件,比如不允许直接访问/ js /文件夹中的js但允许它从我的php页面访问

我在/ js /文件夹中添加了以下代码的.htaccess文件,

Order deny,allow
Deny from all

所以它不允许我直接通过浏览器访问它是好的!

但是当我尝试使用tag访问包含/ js / folder文件的index.php页面时,它没有加载。

所以任何人都可以帮助我!

提前致谢。

3 个答案:

答案 0 :(得分:3)

您没有从PHP页面访问它""。 Web服务器要么提供请求,要么不是。当您加载" PHP页面时#34;在浏览器中,浏览器将出去并请求从您的页面链接到的所有Javascript,CSS和图像资源。其中每个都是对Web服务器的单独HTTP请求。 Web服务器没有此HTTP请求的上下文,因为资产链接到"您的PHP页面",这与它完全无关。您可以使用HTTP请求从服务器获取文件,也可以不使用。通过设置Deny from all,您可以

您必须通过PHP文件汇集所有请求,该文件检查登录信息,并且仅在用户正确登录时才提供文件。链接到scripts.php?file=js/myscript.js并在scripts.php中包含身份验证检查代码。

答案 1 :(得分:0)

限制此类文件夹时,不能在HTML页面中包含其中的文件。它基本上是相同的请求,就好像该人通过URL直接访问JS一样。

答案 2 :(得分:0)

通常是cpanel工具,如

  1. Hotlink Protection

    Hotlink protection prevents other websites from directly linking to files (as specified below) on your website. Other sites will still be able to link to any file type that you don't specify below (i.e., HTML files). An example of hotlinking would be using an <img> tag to display an image from your site somewhere else on the Web. The end result is that the other site is stealing your bandwidth.

  2. 索引管理器

    The Index Manager allows you to customize the way a directory will be viewed on the web. You can select between a default style, no indexes, or two types of indexing. If you do not wish for people to be able to see the files in your directory, choose "No Indexing".

  3. 1&amp; 2是来自常规托管cpanel的工具。可能它写的是apache conf文件(不确定哪些)

    但是,您还应该了解HTTP referer。您可以根据此决定何时不显示您的资源。

    `HTTP referer is an HTTP header field that identifies the address of the webpage (i.e. the URI or IRI) that linked to the resource being requested`