1

I want to prevent direct .php file access. This below 2 lines works fine

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php - [F,L]

But, it won't open index.php file. I want to prevent direct .php file access except index.php file

Karuppiah RK
  • 3,894
  • 9
  • 40
  • 80

4 Answers4

1

Add a condition to not apply the rule to the specific file:

RewriteCond $1 !^(index\.php)$
Paul
  • 8,974
  • 3
  • 28
  • 48
1

To prevent direct access to all .php files except index.php you can use this rule:

RewriteCond %{THE_REQUEST} \.php[?/] [NC]
RewriteRule !^index\.php$ - [F,L,NC]

You need to use %{THE_REQUEST} variable for this. THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.

anubhava
  • 761,203
  • 64
  • 569
  • 643
0
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule !^index\.php$ - [F,L]
Cesar Bielich
  • 4,754
  • 9
  • 39
  • 81
0
Options +FollowSymlinks
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !^(.+).css$ 
RewriteCond %{REQUEST_FILENAME} !^(.+).js$ 
RewriteCond %{REQUEST_FILENAME} !index.php$ 
RewriteRule ^(.+)$ /deny/ [nc] 

Access to files "CSS" and "JS" and "index.php" will be there. Other requests will be redirect to "Deny" folders.

MONJE
  • 85
  • 11