删除.php和.html扩展名,并具有相同名称的目录? [英] Remove .php and .html extensions AND have directories with same name?

查看:59
本文介绍了删除.php和.html扩展名,并具有相同名称的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在浏览器中删除网站页面的.php和.html扩展名。我在网上找到了此代码以添加到.htaccess文件中:

I want to remove the .php and .html extensions of my website pages in the browser. I found this code online to add in the .htaccess file:

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

但是,如果a有一个名为 home.php的php文件,并且同时有一个名为 home的目录,那么它将不再起作用。我想我可能必须添加

However, if a have a php file named "home.php" and at the same time a directory named "home" this doesn't work anymore. I think I might have to add something like

Options All -Indexes -MultiViews

但是我喜欢它也不起作用。
那么我该如何实现呢?

but I like this it does not work either. So how can I achieve this?

推荐答案

当请求到来时,您可以为文件赋予优先级没有 / 且具有相同名称的目录,并删除 php& html 并确定首先检查哪个对象:

You could be able to give a priority to file when it request comes without / with same name with directory as well as removing both php & html and determine which one to check first like this :

DirectorySlash Off
RewriteEngine on 
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(php|html)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME}   !-f 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*) /$1.php [L]
RewriteCond %{REQUEST_FILENAME}   !-f 
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*) /$1.html [L]
RewriteCond %{REQUEST_FILENAME}   !-f 
RewriteCond %{REQUEST_URI} !\/$
RewriteRule ^(.*) %{REQUEST_URI}/ [L,R=302]

因此,通过上述代码,扩展名将被删除,然后将首先进行检查如果有匹配的php文件,则是html,最后是目录。

So , by the code above , extensions will be removed then will check first if there is php file match this , then html and finally if there is a directory .

因此,请求 / home 仅将首先检查名为home的php是否存在,然后目录home但请求 / home / a bout 会直接转到主目录内。

So , the request for /home only will check first a php that named home if it is exist ok , then directory home but the request /home/about will go directly to about inside home directory.

清除浏览器缓存,然后对其进行测试,如果确定,请更改 302 301 以获得永久重定向

Clear browser cache then test it , if it is Ok , change 302 to 301 to get permanent redirection

这篇关于删除.php和.html扩展名,并具有相同名称的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆