Reel Mark
Reel Mark

Reputation: 21

PHP - how to prevent .php files from direct access

i have following folders:

mywebsite/en/index.php
mywebsite/en/product/info.php
mywebsite/en/tags/tags.php
mywebsite/en/games/list.php

my site is running on friendly url, top mentioned files accessable from following friendly urls

mywebsite/en/?pageno=1
mywebsite/en/product/Nike-glove
mywebsite/en/tags/Nike
mywebsite/en/games/ice-sports

i have some examples, but nothing work for me like...i want to prevent direct access to .php files..i want to do that with htaccess...can i?

thanks and kind regards

Reelmark

Upvotes: 0

Views: 4304

Answers (5)

Adrian Günter
Adrian Günter

Reputation: 911

The solution I've settled on is based on @Fou's answer, but I've modified it to handle the case of /script.php/oops and opted to rewrite the response code as 404 instead of using a temporary redirect:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\.php[/?\ ]
RewriteRule .* - [R=404]

Upvotes: 1

messerchainey
messerchainey

Reputation: 97

just add this into your htaccess file... give it a try :) hope it useful

RewriteEngine On
IndexIgnore *

Upvotes: 0

Fou
Fou

Reputation: 916

add 404 page and try following...

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\.php[/?\ ]
RewriteRule .*\.php$ 404.php [L]

Upvotes: 5

Jon Lin
Jon Lin

Reputation: 143846

Not sure exactly sure what you are trying to accomplish, but this will prevent direct access of your php files:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[^\ ]+\.php($|\ )
RewriteRule \.php$ / [F,L]

Though I'm guessing you really want to redirect to the friendly urls when accessing the php files directly.

Upvotes: 2

ckaufman
ckaufman

Reputation: 1497

<Files ~ "\.php$">
Order allow,deny
Deny from all
</Files>

Upvotes: 1

Related Questions