The method of preventing PHP files from being accessed directly under Apache

Source: Internet
Author: User

In the beginning, I wanted to directly prohibit the URL of the PHP suffix from being accessed in the rewrite rules. But it turns out that rewrite rules are recursively invoked, and if PHP is banned directly in rewrite rules, then rules that rewrite to PHP files will fail. Rewriteengineon

REWRITERULE^TEST$/TEST.PHP[L]

REWRITERULE^TEST.PHP$$0[F,L]

Recursive call this is terrible, when you start to access the/test URL rewrite is checked once and then matched to ^test$ for internal redirection to/test.php, however internal redirection also triggers URL rewriting, so check again, match to ^test.php$, be forced directly [F] ( Forbidden) operation, so it becomes a 403 error. In this case, you must determine whether you have been redirected to the server. There's a redirect_url in the server variable, so I try to use that as a judge.

Rewriteengineon

REWRITERULE^TEST$/TEST.PHP[L]

rewritecond%{redirect_url}^$

REWRITERULE.*$0[F,L] Such write access/test is still 403, a little check, found that Rewritecond%{redirect_url} is always empty, this is the egg pain, so that in the rewrite rules can not directly prohibit PHP. But it can be accomplished in a less gorgeous way. is in the PHP file to Judge Redirect_url, although this method can be achieved, but the feeling is very lame, but so far did not find any better way.

$_server[' Redirect_url ']ordie (' forbidden ');

Here is only the display of text, the actual use of the time you need to output the HTTP error code.

echo$_server[' Redirect_url '];//successful access to display information

?>

This section of the PHP code into the global reference is basically no problem, although not a perfect solution, but at least it is resolved, later may find a better way.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.