简体   繁体   English

PHP-如何防止.php文件直接访问

[英]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? 我有一些示例,但是对我没有任何帮助,就像...我想防止直接访问.php文件..我想使用htaccess做到这一点...可以吗?

thanks and kind regards 谢谢和亲切的问候

Reelmark Reelmark

add 404 page and try following... 添加404页面并尝试以下操作...

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

Not sure exactly sure what you are trying to accomplish, but this will prevent direct access of your php files: 不完全确定您要完成什么,但这将阻止直接访问您的php文件:

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. 虽然我猜您真的很想直接访问php文件时重定向到友好的url。

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

just add this into your htaccess file... give it a try :) hope it useful 只需将其添加到您的htaccess文件中...试试看:)希望它有用

RewriteEngine On
IndexIgnore *

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: 我确定的解决方案基于@Fou的答案 ,但我已经对其进行了修改以处理/script.php/oops的情况,并选择将响应代码重写为404而不是使用临时重定向:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM