在 url mod_rewrite 中隐藏扩展名 .php
我有一个 PHP 网站,我正在尝试隐藏扩展程序.我通过谷歌找到了一些东西,但它们似乎都太复杂了,或者它们将 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
您重定向 url 中具有相同名称的任何 php 文件而没有php".
然后在您的 php 文件中,您可以检查 url 是否包含扩展名 (http://blabla.com/bla.php) 并将页面重定向到没有扩展名的相同页面.
所以,在每个 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));
}
相关文章