0

I'm trying to make a "nice URL" using .htaccess file. I have already written a code that works and is stable, but I can not delete the extension .html. I wrote a code that removes the extension .html, but sometimes it causes error 500. I also wrote a code that after entering the URL without extension .html opens the page content, but at the address with the added .html is also the same content (the page is doubled). I want to enter the contents of the abc.html page after entering the address http://example.com/abc (but the address bar still has to be http://example.com/abc. After entering the address http://example.com/ abc.html I want to redirect to http://example.com/abc (this address should be in the address bar.).

My last question related to this topic

My current .htaccess file:

# rewrite engine initiation
RewriteEngine on

RewriteBase /

#canceling www
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# handle trailing slashes (if not a directory)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R,L]

#canceling index or index.html
RewriteRule ^index.(php|html|htm)$ / [R=301,L,NC]
RewriteRule ^index$ / [R=301,L,NC]

What should I add to this code to get rid of the .html extension (so that it works always and does not cause errors)?

Edit: My problem is more specific. Solutions found in other topics are not working. I do not want the contents of the abc.html file to be available at exaple.com/abc.html and example.com/abc. It should be available only under example.com/abc.

  • Possible duplicate of [Remove .php extension with .htaccess](https://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess) – simon Feb 02 '18 at 12:45

1 Answers1

0

Try this rule:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

This will rewrite all requests that can be mapped to an existing file when appending a .php

Shishil Patel
  • 3,449
  • 2
  • 12
  • 16
  • I've tried similar solutions. It does not work. When you enter http://example.com/abc the content of abc.html opens, but when you enter http://example.com/abc.html redirection does not work. – Maciej Rzepinski Feb 02 '18 at 12:44