简体   繁体   English

使用.htaccess删除和禁止扩展

[英]remove and disallow extensions with .htaccess

I found this script or removing extensions from php files: 我找到了这个脚本或从php文件中删除了扩展名:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

While this works, it still allows files to be accessed if you type them in with their extension, which I don't want. 尽管此方法有效,但如果您使用扩展名键入文件,它仍然允许访问文件,我不希望这样做。

I tried the following: 我尝试了以下方法:

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

I also tried ^(.*)$ and ^(.+)$ 我也尝试了^(.*)$^(.+)$

which seems like it should do the job, because it would do this: 看起来应该可以完成这项工作,因为它将这样做:

index.php -> index.php.php

but somehow, it doesn't work as expected. 但不知何故,它无法按预期工作。

So how do I update the above .htaccess script to disallow file extensions? 那么,如何更新上述.htaccess脚本以禁止文件扩展名?

EDIT: 编辑:

My .htaccess script seems to be detecting the files correctly: 我的.htaccess脚本似乎正在正确检测文件:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule "^.*\.php$" "other.html" [L]
RewriteRule "^([^\.]+)$" "other2.html" [L]

The pages other.html and other2.html simply contain the words "OTHER" and "OTHER 2" 页面other.html和other2.html仅包含单词“ OTHER”和“ OTHER 2”

Now, using the above script, the output is as expected: 现在,使用上面的脚本,输出是预期的:

"/test.php" gives output "OTHER"
"/test" gives "OTHER 2"

but if I update the script to the following, both url variations start returning "OTHER" 但是,如果我将脚本更新为以下内容,则两个网址变体都开始返回“ OTHER”

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

So it seems that after the extensionless filename has ".php" added to it by rule#2, it somehow gets caught by rule #1. 因此,似乎无扩展名的文件名在规则2中添加了“ .php”后,就会以某种方式被规则1捕获。

Aren't these rules ordered? 这些规则不是命令吗? and isn't [L] supposed to stop processing on a match? 并且[L]不应该在比赛中停止处理吗?

EDIT 2: 编辑2:
So assuming [L] did what I was expecting... the following script would work... 因此,假设[L]了我的期望...以下脚本将起作用...

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

Try this solution How to remove file extension from website address? 尝试此解决方案如何从网站地址中删除文件扩展名?

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

edit: you can do a filematch to prevent executing all .php except index.php. 编辑:您可以进行文件匹配以阻止执行除index.php之外的所有.php。 I typically route most of my pages through index.php anyways so this solution would work if you only have a handful of base index files (index1.php,index2.php,etc). 无论如何,我通常都会通过index.php路由我的大多数页面,因此,如果您只有少数基本索引文件(index1.php,index2.php等),则此解决方案将起作用。 I'm sure you can come up with something from this if you don't find a working solution. 如果您找不到可行的解决方案,我敢肯定您可以从中提出一些建议。

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

This works: 这有效:

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

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

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