簡體   English   中英

.htaccess 刪除擴展名

[英].htaccess removing of extensions

我已經看到很多關於 .htaccess 刪除擴展名的問題,並且發現了一個通過在 .htaccess 文件中使用它來從擴展名中刪除 .php 的問題:

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

我的問題是我的擴展名是 my-domain/themes/smooth/index.php 並且在 .htaccess 文件中使用它只會將其縮短為 my-domain/themes/smooth/index 和其他頁面,例如聯系我們看起來像我的-域/主題/平滑/聯系。

有沒有辦法去除擴展的中間部分,使其看起來更像:

my-domain/contact

是的,有,但 Symphony、codeigniter 等框架將幫助您做到這一點。 如果您正在尋找一個簡單的實現,那么您可以重新映射文​​件名。

$uri = trim($_SERVER['REQUEST_URI'], '/');

$pages = array(
   'contact' => 'path to contactus.php',
   'services' => ' path to services.php'
);

if ( in_array($uri, array_keys($pages))){
   include $pages[$uri];
}

我沒有測試過這段代碼。 總之,您有一個 index.php 頁面,該頁面包含其他頁面。

您可以在 root .htaccess 中使用此規則來隱藏themes/smooth/.php

RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{DOCUMENT_ROOT}/themes/smooth/$1\.php -f [NC]
RewriteRule ^(.*)$ /themes/smooth/$1.php [L]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM