Hide extension .php in url mod_rewrite

I have a website in PHP and am trying to hide the extension. I found a couple things through Google, but they all seem too complicated or redirect index to index.php : for example, if you write name.com/contact , it goes to name.com/contact.php . I do not want this, 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 

the /en/ folder is the English version, and the /es/ folder is the Spanish version

Also, links on the website will also be without extension.

For example, the image associated with clientes will be clientes , not clientes.php and if someone tries to go to /page.php , it still works but it redirects to /page .

+1
source share
3 answers

The htacces file should look like this:

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

You redirect any php file to the url of the same name using "php".
Then in your php files you can check if the url contains the extension (http://blabla.com/bla .php ) and redirects the page to the same one with the extension. <w> 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)); } 
+7
source

Attach the .htaccess file to the root directory using this code!

 Options +FollowSymlinks RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.+)$ /$1.php [L,QSA] 

Hope!! it would be helpful for someone!

0
source
 RewriteEngine on Rewritecond %{REQUEST_URI} !-f Rewritecond %{REQUEST_URI} !-d Rewritecond %{REQUEST_URI} !-l RewriteRule ^([\w\d-]+)$ $1.php [L] RewriteRule ^([^/.]+)$ $1.php [L] 
0
source

All Articles