问题描述
我有一个使用PHP的网站,并且我试图隐藏扩展名.我已经通过Google找到了一些方法,但是它们似乎都太复杂了,或者它们将index
重定向到了index.php
:就像您编写name.com/contact
一样,它也会转到name.com/contact.php
.我不想要,我只想要:
I have a website in PHP, and I'm trying to hide the extension. I've found a couple of things via Google, but they all seem to be too complicated, or they redirect index
to index.php
: like if you write name.com/contact
it goes to name.com/contact.php
. I don't want that, I just want:
www.name.com/es/index.php to be www.name.com/es/inicio
www.name.com/es/empresa.php to be www.name.com/es/empresa
www.name.com/es/productos.php to be www.name.com/es/productos
www.name.com/es/clientes.php to be www.name.com/es/clientes
www.name.com/es/recetas.php to be www.name.com/es/recetas
www.name.com/es/contacto.php to be www.name.com/es/contacto
www.name.com/en/index.php to be www.name.com/en/home
www.name.com/en/company.php to be www.name.com/en/company
www.name.com/en/products.php to be www.name.com/en/products
www.name.com/en/clients.php to be www.name.com/en/clients
www.name.com/en/recepis.php to be www.name.com/en/recepis
www.name.com/en/contact.php to be www.name.com/en/contact
/en/
文件夹是英语版本,/es/
文件夹是西班牙语版本
the /en/
folder is English version and the /es/
folder is the Spanish version
网站内的链接也将没有扩展名.
Also the links inside the website, would be also without the extension.
例如,链接到clientes
的图像将是clientes
,而不是 clientes.php
,并且,如果有人尝试转到/page.php
,它仍然可以工作但它会重定向到/page
.
E.g, an image linked to clientes
would be clientes
and not clientes.php
and if somebody tries to go to /page.php
, it still works but it redirects to /page
.
推荐答案
htacces文件应如下所示:
The htacces file should look like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
您可以重定向网址中具有相同名称的任何php文件,而无需添加"php".
然后,在您的php文件中,您可以检查网址是否包含扩展名(http://blabla.com/bla .php ),并将页面重定向到没有扩展名的同一页面. br>因此,在每个php文件的开头,您应该调用此函数:
You redirect any php file in the url with the same name withouth the "php".
Then in your php files, you can check to see if the url contains the extension (http://blabla.com/bla.php) and redirect the page to the same one withouth the extension.
So, at the beginning of each php file you should call this function :
function redirectIfNeeded(){
$url = $_SERVER["REQUEST_URI"];
if(preg_match("/\.php/$", $url))
header("Location: ".preg_replace("/\.php/",$url));
}
这篇关于隐藏扩展名.php的URL mod_rewrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!