问题:
这是当前文件夹:
/app
/dist
/index.html
/index.css
/index.js
/src
/configurations.json
/index.php
/configurations.php
有敏感数据:
app/src/...
/...
希望用户只能访问:
index.php
app/dist/whateverfile.whateverformat
如何用这个.htaccess
语法完成?
Options -Indexes
DirectoryIndex index.php
<FilesMatch ".*">
Order allow,deny
Deny from all
Satisfy All
</FilesMatch>
<Files index.php>
Order allow,deny
Allow from all
Satisfy All
</Files>
<Files "">
Order allow,deny
Allow from all
Satisfy All
</Files>
答案1:
换句话说,
.
`-- some-path
|-- app
| `-- dist
| |-- index.css
| |-- index.html
| `-- index.js
|-- src
| `-- configurations.json
`-- www.example.com
|-- .htaccess
`-- index.php
# /some-path/www.example.com/.htaccess
# exposes/some-path/app/ directory as the URI path `http://www.example.com/app/`
Alias "/app" "/some-path/app"
Alias "/app/dist" "/some-path/app/dist"
答案2:
<Files>
和<FilesMatch>
指令只用于目标文件,而不是目录,如果可以访问服务器配置,那么你可以使用<Directory>
容器。
使用mod_rewrite
在.htaccess
中,可以使用mod_rewrite限制对/index.php
或/app/dist/
以外的对象的访问。
例如:
RewriteEngine On
RewriteRule !^(index.php$|app/dist/) - [F]
对于不是/index.php
或不启动/app/dist/
的请求,上面的响应将禁用403,如果要返回404,请将F
更改为R=404
。
如果只想对映射到实际文件或目录的请求使用403响应(否则为404 ),则添加两个检查文件系统的条件(但请注意,文件系统检查相对昂贵,因此除非你需要,否则不要使用它们),例如:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !^(index.php$|app/dist/) - [F]
仅当请求的URL不在允许的"组"中并且映射到文件或目录时,RewriteRule
指令才会被操作。
但是,我假定你的URL实际上不包含index.php
(是),在这种情况下,用户请求/
和mod_dir内部问题,在这种情况下,你需要使index.php
为可选。
例如:
RewriteRule !^((index.php)?$|app/dist/) - [F]
使用Apache Expression
或者,你可以使用一个Apache表达式(Apache 2.4 )
<If "%{REQUEST_URI} !~ m#^/((index.php)?$|app/dist/)#">
Require all denied
</If>
相关文章