exclude php files or a whole directory from codeigniter

Viewed 454

I have some php files in the root directory and a directory containing some php files which I want to exclude from codeigniter. I have this code in htaccess file

RewriteCond %{REQUEST_URI} !^subdomain_name\. [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$

RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^/(directory_to_exclude)$

RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$

RewriteRule ^(.*)$ index.php/$1 [L]  

When I navigate to example.com/directory_to_exclude I get a list of php files (I'll add an index to prevent listing) but when I try to run any of them it reloads the codeigniter homepage. So how to fix this and how to exclude any php file in the root directory like example.com/test.php

1 Answers

Change your last rule to this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} !\s/+[^/.]+\.php[?\s/] [NC]
RewriteCond %{THE_REQUEST} !\s/+directory_to_exclude/ [NC]
RewriteCond %{THE_REQUEST} !\s/+[0-9]+\..+\.cpaneldcv [NC]
RewriteRule ^(.*)$ index.php/$1 [L]  

RewriteCond %{THE_REQUEST} !\s/+[^/.]+\.php[?\s/] [NC] will exclude all .php files in site root directory.

You shoud use THE_REQUEST instead of REQUEST_URI because it doesn't get overwritten after execution of some rewrite rules.

EDIT: If this doesn't work then change file permission to 644. (this actually solved OP's problem)

Related