防止' index.php'使用Slimframework

时间:2017-02-11 03:24:04

标签: php .htaccess slim

我使用Slimframework作为我的网络应用程序&我有所有url请求指向" index.php'中的一个入口点在我的.htaccess中使用以下ReWrite:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

一切都很好,但是,index.php仍可通过以下请求/路由在网址中访问: 我不想要的/mywebsite/public/index.php。现在我明白它是我公共html文件夹中的唯一文件,但我不希望它在URL中可访问...如何通过

将其重定向到错误的路径

1).htaccess ReWrite

或者

2)一些苗条的逻辑

???

2 个答案:

答案 0 :(得分:3)

您可以像这样更新规则:

RewriteEngine on
# Rewrite to index.php
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^ index.php [L,E=rewritten:1]
# 404 any direct access of index.php
RewriteCond %{ENV:rewritten} !=1
RewriteCond %{ENV:REDIRECT_rewritten} !=1
RewriteRule ^index\.php$ - [R=404,L]

我删除了不需要的QSA标志。我删除了文件和目录检查,这是不必要的,并替换为index.php检查更快。

要回答您的实际问题,我会为任何尝试直接访问index.php返回404。另一种选择是重定向到某个东西。

答案 1 :(得分:2)

如果要禁用对index.php的外部访问,请将以下代码置于现有规则之上

RewriteEngine on

RewriteCond %{THE_REQUEST} \s/index\.php\s [NC]
RewriteRule ^ - [F]

如果直接访问 /index.php ,这将返回Forbidden错误。