[英]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.