简体   繁体   English

.htaccess 删除扩展名

[英].htaccess removing of extensions

I have seen many question been asked about the .htaccess removal of extension and have found one that removes the .php from the extension by using this in the .htaccess files:我已经看到很多关于 .htaccess 删除扩展名的问题,并且发现了一个通过在 .htaccess 文件中使用它来从扩展名中删除 .php 的问题:

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

My problem is that my extension is my-domain/themes/smooth/index.php and having that in the .htaccess file only shortens it to my-domain/themes/smooth/index and other pages like the contact us look like my-domain/themes/smooth/contact.我的问题是我的扩展名是 my-domain/themes/smooth/index.php 并且在 .htaccess 文件中使用它只会将其缩短为 my-domain/themes/smooth/index 和其他页面,例如联系我们看起来像我的-域/主题/平滑/联系。

Is there any way of having the middle of the extension removed so that it look more like:有没有办法去除扩展的中间部分,使其看起来更像:

my-domain/contact

Yes, there is, but frameworks such as Symphony , codeigniter etc will help you do that.是的,有,但 Symphony、codeigniter 等框架将帮助您做到这一点。 If your looking for a simple implementation, then you can do a remap of the file names.如果您正在寻找一个简单的实现,那么您可以重新映射文​​件名。

$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];
}

Ive not tested this code.我没有测试过这段代码。 In summary you have an index.php page and that page includes other pages.总之,您有一个 index.php 页面,该页面包含其他页面。

You can use this rule in root .htaccess to hide themes/smooth/ and .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