【问题标题】:Forbid access to directory, but not alias?禁止访问目录,但不禁止别名?
【发布时间】:2021-05-17 23:12:09
【问题描述】:
使用 Apache。当 /storage/ 是 /.storage/subdomain/ 的别名时,如何允许访问 /storage/ 但拒绝访问 /.storage/?
# Define alias based on subdomain
RewriteCond %{HTTP_HOST} ^([^.]+)$
RewriteRule ^storage/(.*)$ ".storage/%1/$1" [QSA,L]
# Forbid access to
RewriteCond %{REQUEST_URI} ^/\.storage/
RewriteRule ^ - [F,L]
【问题讨论】:
标签:
apache
.htaccess
mod-rewrite
httpd.conf
apache2.4
【解决方案1】:
根据您显示的示例,您能否尝试以下操作。请确保在测试 URL 之前清除浏览器缓存。我们需要先修复正则表达式以获取子域,然后我们需要从规则中更改 " 并添加一个附加条件来检查以确保用户无法直接访问实际文件夹。
RewriteEngine ON
# Define alias based on subdomain
RewriteCond %{HTTP_HOST} ^(?:www\.)(.*)$ [NC]
RewriteRule ^storage/(.*)$ .storage/%1/$1 [NC,QSA,L]
# Forbid access to
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^\.storage - [F,NC,L]