Using ModRewrite to get rid of extensions

I would like to get rid of all the file extensions on my site. Unless they are on the index, I would like it to say nothing ...

change this foo.com/index.html
to this foo.com/

and when the user goes to another page, for example, foo.com/contact-us.html
it will be foo.com/contact-us

RewriteEngine On
RewriteRule ^ this is where i get confused :(

Thanks in advance!

+3
source share
3 answers

Try the following rules:

RewriteRule ^index\.html$ / [L,R=301]
RewriteRule (.+)\.html$ /$1 [L,R=301]
+3
source

I would use a slightly different method:

RewriteCond %{REQUEST_FILENAME}\.html -f # if request.html exists and is a file
RewriteRule ^(.+)$ $1\.html [QSA] # then rewrite to that file.
+2
source

, apache , :

Just add this rule to your .htaccess and / contact -us /, contact-us.html will be automatically rewritten:

Options MultiViews

What is it!

0
source

Source: https://habr.com/ru/post/1725255/


All Articles