.htaccess,包含两个子文件夹(位于根目录中的基本目录)



在本地服务器的webroot中,我尝试在其自身目录中安装Bedrock:

|- htdocs
|- bedrock
|- web

使用我编写的.htaccess文件,我可以访问web目录,从而启动我的应用程序:

RewriteEngine On

RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond %{HTTP_HOST} ^(www.)?example.local$
RewriteCond %{REQUEST_URI} !^/bedrock/web/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /bedrock/web/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.local$
RewriteRule ^(/)?$ bedrock/web/index.php [L]

不过,我希望避免访问url为example.local/bedrock的基岩目录。

我可以用Apache的重写模块来实现这一点吗?还是必须在基岩目录中添加一个带有deny from all的.htaccess?

您可以阻止对/bedrock子目录的直接访问,但仍然允许请求在内部重写到该目录,方法是在现有规则之前添加以下

# Block "direct" access to the "/bedrock" subdirectory
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^bedrock($|/) - [F]

以上将返回一个";403禁止";如果客户端直接请求CCD_ 4或CCD_。

通过检查REDIRECT_STATUS环境变量是否为,我们可以确保该规则仅应用于直接请求(而不是重写请求-稍后在文件中(。

REDIRECT_STATUSenv在初始请求时为空,但在第一次成功重写后设置为200(如200 OK状态(。

。。。还是我必须在基岩目录中添加一个带有deny from all的.htaccess?

这将阻止所有请求,包括您的内部重写。


旁白:

RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond %{HTTP_HOST} ^(www.)?example.local$
RewriteCond %{REQUEST_URI} !^/bedrock/web/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /bedrock/web/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.local$
RewriteRule ^(/)?$ bedrock/web/index.php [L]

您应该在前两个规则中包含L标志。您只在最后一条规则中包含了L标志(这不是严格要求的(。

您不需要最后一条规则中的捕获组。^/?$(或者甚至.htaccess中的^$(就足够了。