Search code examples
phpcodeignitercodeigniter-3

Prevent direct access to any PHP files on codeigniter


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


Solution

  • 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. :)