htaccess允许使用正则表达式的文件



我使用htaccess拒绝访问我的php文件。尽管如此,我还是需要从外部调用其中的一些,例如index.php

我想出了

<Files a_string_a.php>
Order Allow,Deny
Allow from all
</Files>

工作如预期,我可以使用该文件。但是我有几个档案。我想避免为每个文件写这样的块。有没有机会解释htaccess,它将允许访问所有与匹配的文件

..._string_.....PHP? (containing a unique string no matter what else is in the filename)

我很确定有一个RegExpress可以解决这个问题,但我有点不知所措,不起诉如何将其集成到htaccess中——谢谢你的提示。

可能是这样的:

<FilesMatch "(index|foo|bar).php$">
Order Allow,Deny
Allow from all
</FilesMatch>

并使用|(regex或(添加任何文件基名称。

请参阅https://httpd.apache.org/docs/2.4/mod/core.html#filesmatch

..._string_.....PHP?(无论文件名中还有什么,都包含一个唯一的字符串(

您可以将此FilesMatch指令与regex:一起使用

<FilesMatch "(?i)^.*_string_.*.php$">
Order Allow,Deny
Allow from all
</FilesMatch>

RegEx详细信息:

  • (?i):启用忽略大小写匹配
  • ^:启动
  • .*:匹配0个或多个任意字符
  • _string_:匹配_string_
  • .*:匹配0个或多个任意字符
  • .php:匹配.php
  • $:结束

最新更新