Php 使用.htaccess删除和禁止扩展

Php 使用.htaccess删除和禁止扩展,php,.htaccess,Php,.htaccess,我发现了以下脚本或正在从php文件中删除扩展名: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L] 虽然这样做有效,但如果您使用扩展名键入文件,它仍然允许访问这些文件,我不希望这样做 我尝试了以下方法: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([.]+)$ $1.php [

我发现了以下脚本或正在从php文件中删除扩展名:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
虽然这样做有效,但如果您使用扩展名键入文件,它仍然允许访问这些文件,我不希望这样做

我尝试了以下方法:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([.]+)$ $1.php [NC,L]
我还尝试了
^(.*)$
^(+)$

它似乎应该做这项工作,因为它会这样做:

index.php -> index.php.php
但不知怎么的,它并没有像预期的那样工作

那么,如何更新上述.htaccess脚本以禁止文件扩展名

编辑:

My.htaccess脚本似乎正在正确检测文件:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule "^.*\.php$" "other.html" [L]
RewriteRule "^([^\.]+)$" "other2.html" [L]
页面other.html和other2.html只包含单词“other”和“other 2”

现在,使用上述脚本,输出与预期一致:

"/test.php" gives output "OTHER"
"/test" gives "OTHER 2"
但是如果我将脚本更新为以下内容,两个url变体都会开始返回“OTHER”

因此,似乎在无扩展文件名根据规则2添加了“.php”之后,它不知何故被规则1捕获

这些规则不是有条理的吗?难道[L]不应该停止处理比赛吗

编辑2:
所以假设
[L]
做了我所期望的。。。以下脚本将起作用

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule "^.*\.php$" "404.html" [L]
RewriteRule "^([^\.]+)$" "$1.php" [L]
试试这个解决方案

编辑:您可以进行文件匹配以防止执行除index.php之外的所有.php。我通常通过index.php路由我的大部分页面,因此如果您只有少数基本索引文件(index1.php、index2.php等),这个解决方案就可以工作。如果你找不到有效的解决办法,我相信你可以从中想出一些办法

<FilesMatch "\.php$">
    Order Allow,Deny
    Deny from all
</FilesMatch>
<FilesMatch "index[0-9]?\.php$">
    Order Allow,Deny
    Allow from all
</FilesMatch>

命令允许,拒绝
全盘否定
命令允许,拒绝
通融
这项工作:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule "^.*\.php$" "404.html" [END]
RewriteRule "^([^\.]+)$" "$1.php" [END]

这似乎仍然不会阻止使用扩展名访问文件。我不是专家,但您可以尝试类似于
RewriteRule^.php$404.html的方法。还尝试了
(.*)\.php$404.html
试试这个,我发现它是有效的:
RewriteRule上的RewriteEngine^(.*)。当我使用它时,php$404.html
页面很简单。它不允许我使用“/test”来测试。。随机的否决票。。希望有人少一点悲伤。。
<FilesMatch "\.php$">
    Order Allow,Deny
    Deny from all
</FilesMatch>
<FilesMatch "index[0-9]?\.php$">
    Order Allow,Deny
    Allow from all
</FilesMatch>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule "^.*\.php$" "404.html" [END]
RewriteRule "^([^\.]+)$" "$1.php" [END]