.htaccess删除扩展名

时间:2014-05-18 13:13:05

标签: php .htaccess file

我已经看到很多关于.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 / themes / smooth / contact。

是否有任何方法可以删除扩展名的中间部分,使其看起来更像:

我的域/接触

最好的问候&非常感谢

2 个答案:

答案 0 :(得分:1)

是的,有,但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页面,该页面包含其他页面。

答案 1 :(得分:0)

您可以在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]