使用 .Htacess 檔案刪除 PHP 副檔名

Kevin Amayi 2023年1月30日 2022年5月13日
  1. 在 PHP 中使用 .htaccess 檔案刪除 PHP 擴充套件
  2. 在 PHP 中使用帶有條件語句.htaccess 檔案刪除 PHP 擴充套件
使用 .Htacess 檔案刪除 PHP 副檔名

我們將研究使用 .htaccess 檔案刪除 PHP 擴充套件的標準方法。你還需要從安裝了 PHP 的 Apache 之類的伺服器執行此程式碼。

我們還將使用帶有條件語句的 .htaccess 檔案刪除 PHP 擴充套件,以防止在刪除 PHP 擴充套件時出現不必要的迴圈。

你還需要從安裝了 PHP 的 Apache 之類的伺服器執行此程式碼。

在 PHP 中使用 .htaccess 檔案刪除 PHP 擴充套件

#remove php file extension-e.g. https://example.com/file.php will become https://example.com/file 
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

在 PHP 中使用帶有條件語句.htaccess 檔案刪除 PHP 擴充套件

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]

相關文章 - PHP File