<p class="has-line-data">要通过伪静态规则禁止上传目录 <code>uploadfile</code> 下的 PHP 脚本执行权限,请根据你的服务器类型(<strong>Apache</strong> 或 <strong>Nginx</strong> 或 **IIS选择以下配置方法:</p><hr/><h3 class="code-line"><a></a><strong>一、Apache 服务器(使用 <code>.htaccess</code> 文件)</strong></h3><ol class=" list-paddingleft-2"><li><p class="has-line-data">在 <code>uploadfile</code> 目录下创建或修改 <code>.htaccess</code> 文件。</p></li><li><p class="has-line-data">添加以下规则(根据 Apache 版本选择配置):</p><p class="has-line-data"><strong>Apache 2.4+ 语法:</strong></p></li></ol><pre class="brush:bash;toolbar:false"> <IfModule mod_authz_core.c>
<FilesMatch "\.php$">
Require all denied
</FilesMatch>
</IfModule></pre><ol class=" list-paddingleft-2"><li><p class="has-line-data"><strong>Apache 2.2 及以下语法:</strong></p></li></ol><pre class="brush:bash;toolbar:false"> <FilesMatch "\.php$">
Deny from all
</FilesMatch></pre><ol class=" list-paddingleft-2"><li><p class="has-line-data">确保服务器允许 <code>.htaccess</code> 覆盖配置(主配置需设置 <code>AllowOverride All</code>)。</p></li></ol><hr/><h3 class="code-line"><a></a><strong>二、Nginx 服务器</strong></h3><ol class=" list-paddingleft-2"><li><p>在 伪静态配置中添加以下规则:</p></li></ol><pre class="brush:bash;toolbar:false"> location ^~ /uploadfile/ {
location ~ \.php$ {
deny all;
return 403;
}
}</pre><hr/><h3 class="code-line"><a></a><strong>三、IIS服务器通过 <code>web.config</code> 文件配置</strong></h3><ol class=" list-paddingleft-2"><li><p class="has-line-data"><strong>在 <code>uploadfile</code> 目录下创建或修改 <code>web.config</code> 文件</strong>,内容如下:</p></li></ol><pre class="brush:bash;toolbar:false"> <?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<!-- 拒绝所有对 PHP 文件的请求 -->
<security>
<requestFiltering>
<fileExtensions>
<add fileExtension=".php" allowed="false" />
</fileExtensions>
</requestFiltering>
</security>
<!-- 可选:通过 URL 重写返回 403 -->
<rewrite>
<rules>
<rule name="Block PHP in Upload" stopProcessing="true">
<match url="^uploadfile/.*\.php$" />
<action type="CustomResponse" statusCode="403" statusReason="Forbidden" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration></pre><ol class=" list-paddingleft-2"><li><p class="has-line-data"><strong>保存文件</strong>,并确保 IIS 已启用以下模块:</p></li><ul class=" list-paddingleft-2"><li><p><strong>请求过滤(Request Filtering)</strong></p></li><li><p><strong>URL 重写(URL Rewrite)</strong>(如果使用重写规则)</p></li></ul></ol><hr/><h3 class="code-line"><a></a><strong>三、验证配置</strong></h3><ol class=" list-paddingleft-2"><li><p>上传一个测试文件 <code>test.php</code> 到 <code>uploadfile</code> 目录。</p></li><li><p>尝试通过浏览器访问该文件(如 <code>http://yourdomain.com/uploadfile/test.php</code>)。</p></li><li><p>如果返回 <strong>403 Forbidden</strong> 错误,则配置生效。</p></li></ol><hr/>
相关文章