Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache .htaccess-限制对文件的访问,除非由服务器访问_Apache_.htaccess_Redirect_Mod Rewrite_Http Status Code 403 - Fatal编程技术网

Apache .htaccess-限制对文件的访问,除非由服务器访问

Apache .htaccess-限制对文件的访问,除非由服务器访问,apache,.htaccess,redirect,mod-rewrite,http-status-code-403,Apache,.htaccess,Redirect,Mod Rewrite,Http Status Code 403,我正在寻找有关服务器的.htaccess重写规则的帮助 考虑到我有以下结构: index.html foo.html private\ bar.js secret.txt public\ helloworld.js cat.jpg 我需要以下条件: 用户导航到http://example.com,http://example.com/index.html,http://example.com/foo.html或http://examp

我正在寻找有关服务器的.htaccess重写规则的帮助

考虑到我有以下结构:

index.html
foo.html
private\
        bar.js
        secret.txt
public\
       helloworld.js
       cat.jpg
我需要以下条件:

  • 用户导航到
    http://example.com
    http://example.com/index.html
    http://example.com/foo.html
    http://example.com/foo
    应该能够加载而不会出现问题
  • 用户导航到
    http://example.com/private/bar.js
    http://example.com/private/secret.txt
    和此文件夹中的任何其他文件都应被禁止使用403,尽管使用它的html文件应该能够访问这些文件
  • 用户导航到
    http://example.com/public/
    应该能够访问其中的任何内容
  • 最后一点就是让我明白,如果需要的话,我如何能够否定规则。我成功地阻止了文件,除非它们是.html,但当扩展名被省略时,它就不起作用了,而且非常混乱,所以我希望一些专业人士能帮我找到一个干净的方法


    编辑:澄清了要求#2根据所显示的示例,将所有文件包括在私人文件夹中,请尝试以下内容。请确保在测试URL之前清除浏览器缓存。基本上是第二种情况。无论如何,您的第三个条件不需要任何规则(考虑到您没有第三个条件下的任何其他公用文件夹规则)



    第二种解决方案:确保您一次只使用上述规则或以下规则,以下规则认为您只允许
    http://example.com,index.html,foo.html或foo
    URL,根据您的第一个条件,并阻止其他URL。如果是这种情况,则可以尝试以下操作

    RewriteEngine ON
    ##In case you want to forbid anything apart from:
    ##http://example.com, index.html, foo.html OR foo then following.
    RewriteCond ^(index\.html|foo\.html|foo)/?$ [NC]
    RewriteRule ^ - [R=403,L]
    
    ##For forbidding private/bar.js and private/secret.txt here.
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^private/(bar\.js|secret\.txt)/?$ - [R=403,L]
    

    你可以添加一个例子,说明你认为html将如何使用这些秘密文件吗?比如说javascript文件,它可以被html使用,所以我假设它需要受到引用的限制,就像HotlinkingHanks一样。第二个解决方案是我需要的。你能详细说明变量ENV:REDIRECT\u STATUS在做什么吗?@randonoob,
    RewriteCond%{ENV:REDIRECT\u STATUS}^$
    通过检查内部mod\u rewrite变量REDIRECT\u STATUS防止循环,该变量在首次成功内部重定向后设置为200。
    RewriteEngine ON
    ##In case you want to forbid anything apart from:
    ##http://example.com, index.html, foo.html OR foo then following.
    RewriteCond ^(index\.html|foo\.html|foo)/?$ [NC]
    RewriteRule ^ - [R=403,L]
    
    ##For forbidding private/bar.js and private/secret.txt here.
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^private/(bar\.js|secret\.txt)/?$ - [R=403,L]