拒绝直接访问除 index.php 之外的所有 .php 文件

新手上路,请多包涵

我想拒绝直接访问所有 .php 文件,除了一个: index.php

对其他 .php 文件的唯一访问应该是通过 php include

如果可能的话,我希望所有文件都在同一个文件夹中。

更新:

一般规则会很好,所以我不需要浏览所有文件。风险是我忘记了文件或行。

更新 2:

index.php 在文件夹中 www.myadress.com/myfolder/index.php

我想拒绝访问 --- 中的所有 .php myfolder 以及该文件夹的子文件夹。

原文由 Johan 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 345
1 个回答

这是我的 CMS EFFCORE 中的示例。

Apache 2.4 表示法:

 <If "%{REQUEST_URI} -strmatch '*.php'">
    Require all denied
</If>

<If "%{REQUEST_URI} -strmatch '/index.php'">
    Require all granted
</If>

##########################
### SINGLE ENTRY POINT ###
##########################

RewriteEngine On
RewriteRule ^.*$ index.php [L]

NGINX 表示法:

 server {
    listen 127.0.0.1:80;
    server_name 127.0.0.1;
    root /var/www;
    location ~* .*\.php$ {
        deny all;
        error_log off;
    }
  # SINGLE ENTRY POINT
    location / {
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9000;
        include /usr/local/etc/nginx/fastcgi.conf;
        fastcgi_param SCRIPT_NAME /index.php;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    }
}

IIS 7.5+ 表示法:

 <?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="*.php files protection" patternSyntax="Wildcard" stopProcessing="true">
                    <match url="*.php" />
                    <action type="CustomResponse" statusCode="403" subStatusCode="0" statusReason="Forbidden" statusDescription="Forbidden" />
                </rule>
                <rule name="SINGLE ENTRY POINT" patternSyntax="ECMAScript" stopProcessing="true">
                    <match url="^.*$" ignoreCase="true" />
                    <action type="Rewrite" url="index.php/{R:0}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
        <defaultDocument>
            <files>
                <clear />
                <add value="index.php" />
            </files>
        </defaultDocument>
    </system.webServer>
</configuration>

原文由 Maxim Rysevets 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题