0

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

2
  • 5
    Why do you store files in webroot if you don't want them to be accessible? Commented Jul 30, 2012 at 4:03
  • 1
    Put them outside of your document root. It's the safest method. Leave only your main controller script inside the root. Commented Jul 30, 2012 at 4:32

5 Answers 5

5

add 404 page and try following...

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\.php[/?\ ]
RewriteRule .*\.php$ 404.php [L]
Sign up to request clarification or add additional context in comments.

2 Comments

You're really a king!
This will fail on requests like /script.php/oops because your RewriteRule is unnecessarily and erroneously matching on \.php$. RewriteRule .* 404.php [L] is better as we've already pattern matched the request URI with the RewriteCond, and you can even do RewriteRule .* [R=404] to use the same 404 mechanism as any other "unknown resource" making the entire operation transparent.
2

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.

2 Comments

jon how to prevent file having parameters like "website/tag/tag.php?pno=1"
@ReelMark where are these links coming from? You can't just magically make arbitrary links into something else. If you are generating them, change how you're generating them, if they're being linked to from elsewhere, redirect with a 301 to where the content really is.
1
<Files ~ "\.php$">
Order allow,deny
Deny from all
</Files>

Comments

1

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]

Comments

0

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

RewriteEngine On
IndexIgnore *

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.