I have built a site with codeigniter 3.
I want to prevent run all php files directly. imagine that I have a file named test.php in my root.
When the user goes to a site.com/test.php , it is not possible to access it. Not for a specific file but for all PHP files.
I want to access the site only by calling the controls and their methods like : site.com/user/login
I can do it by adding this codes in .htaccess file :
RewriteEngine on
# Block direct access to PHP files except index.php
RewriteCond %{REQUEST_FILENAME} !index\.php
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteRule ^(.*)$ index.php/$1 [L,NC]
Now every request for .php file from url redirects to 404 page. :)
EDIT 2025:
It is better to use modern frameworks like Laravel, etc. or CodeIgniter version 4.