简体   繁体   中英

.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:

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.

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. 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.

You can use this rule in root .htaccess to hide themes/smooth/ and .php :

RewriteEngine on 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM