Apache .htaccess-限制对文件的访问,除非由服务器访问
我正在寻找有关服务器的.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
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/
应该能够访问其中的任何内容编辑:澄清了要求#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]