Hide the . html extension of the files in the url using . htaccess

Asked

Viewed 4,457 times

2

I created some pages .html and put online. In the browser the file extensions are visible. However I want to hide them to make the urls more "friendly".

For example: instead of www.meusite.com/contato.html I want you to appear only www.meusite.com/contato.

Researching (including similar questions here on the site) I saw that this can or should be done through the following .htaccess. In many tutorials or answers appears the code below, but it did not work with me.

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

ps: I would not like to turn my pages .html in .php because they are static pages.


UPDATING:

I put the following code in the . htaccess file:

    RewriteEngine On
    RewriteRule ^([0-9A-Za-z-_]+)$ /$1.html

I took the test with the "contact" page and it worked. In index.html I changed the tag a which makes the link to the contact page as follows: <a href="contato.html">Contato</a> for <a href="contato">Contato</a>. That is, within the site, when linking a page with another, I removed the extensions from the files. Apparently it worked. This is recommended?

  • I had the same problem and solved it in the same way... that’s what several sites indicate, including this tutorial that I found very enlightening https://ajuda.locaweb.com.br/wiki/comandos-uteis-no-htaccess/

1 answer

0

Tries to enter the code below that can also be used for files that have the extension .php you’ll just have to replace .html for .php:

# Reescreve as URLs html
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ $1.html [L]
</IfModule>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.