假设您有一个像这样的PHP项目:
/config/
/lib/
/public/ -- Only this folder is supposed to be public.
但是它完全上传到Apache的公共目录中。由于某些用户可能不知道自己在做什么,因此Web主机仅允许访问公共目录。
是否有PHP / .htaccess解决方案来处理这种情况?
最好不要访问config和lib目录,并且像/img/header.png这样的URI仍然可以使用。
公共目录中还将包含一个.htaccess文件,这会破坏mod_rewrite解决方案的事情吗?
编辑:
尚未能够解决此问题。这是有关手头问题的一些更详细的信息。
在/public/
目录中,我index.php
在单独的目录中有一个文件和其他公共文件,如JavaScript,CSS和Images。我的.htaccess
档案也在这里。该文件包含以下mod_rewrite
代码:
RewriteEngine On
Options +FollowSymlinks
# Set %{ENV:PROTO} variable, to allow rewrites to redirect with the
# appropriate schema automatically (http or https).
RewriteCond %{HTTPS} =on
RewriteRule ^ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^ - [env=proto:http]
# Rewrite www.example.com → example.com
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]
# Block access to all hidden files and directories with the exception of
# the visible content from within the `/.well-known/` hidden directory.
RewriteCond %{REQUEST_URI} "!(^|/)\.well-known/([^./]+./?)+$" [NC]
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\." - [F]
当httpd的公共目录是正确的并且一切正常时,/public/
但我要的是一种可以抵消用户将项目文件直接上传到公共目录中的可能性的方法。
例如,我的目录最终如下。
/public_html/config/
/public_html/lib/
/public_html/public/
我试图.htaccess
在我的项目根目录中使用一个文件来解决这个问题,但是没有任何运气。即使我将流量重定向到/public_html/public/
,.htaccess
in/public_html/public/
也将覆盖mod_rewrite
重定向到的代码/public_html/public/
。
好的; 这就是我想出的。这非常简单。
.htaccess
我的项目根目录中的文件包含以下内容:
RewriteEngine On
RewriteRule ^(.*) public/$1 [L,PT]
然后将以下内容附加到.htaccess
我的项目公用文件夹中。
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L,PT]
它的工作方式似乎与现在上传的位置无关。即使存在非公用文件夹,您也无法访问它们。由于所有请求均发送到index.php
该文件夹,因此不在公用文件夹中。
我唯一不喜欢的是您仍然可以添加/public/
到URI并获取同一页面。这不像我将在我的公共目录中有第二个公共目录,所以这应该不是问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句