Rápli András
Rápli András

Reputation: 3923

Deny direct access to php files

I wrote this code to redirect everything to index.php.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA]

I want to somehow adjust !-f to allow .php files to be 301 redirected to / without getting into a redirection loop. I have /foo/bar/218 that loads index.php which requires view/foo_bar.php. I want /view/foo_bar.php to be inaccessible via the address bar.

Upvotes: 1

Views: 55

Answers (1)

anubhava
anubhava

Reputation: 784998

You can try these rules:

# redirect .php requests to /
RewriteCond %{THE_REQUEST} \.php[\s?] [NC]
RewriteRule ^ / [L,R=302]

# existing rules without .php files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.php$ index.php [QSA]

Upvotes: 1

Related Questions