[英]remove and disallow extensions with .htaccess
我找到了这个脚本或从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脚本以禁止文件扩展名?
编辑:
我的.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"
但是,如果我将脚本更新为以下内容,则两个网址变体都开始返回“ OTHER”
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule "^.*\.php$" "other.html" [L]
RewriteRule "^([^\.]+)$" "$1.php" [L] // changed
因此,似乎无扩展名的文件名在规则2中添加了“ .php”后,就会以某种方式被规则1捕获。
这些规则不是命令吗? 并且[L]不应该在比赛中停止处理吗?
编辑2:
因此,假设[L]
了我的期望...以下脚本将起作用...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule "^.*\.php$" "404.html" [L]
RewriteRule "^([^\.]+)$" "$1.php" [L]
尝试此解决方案如何从网站地址中删除文件扩展名?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
编辑:您可以进行文件匹配以阻止执行除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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.