Trouble removing .html URL extension using .htaccess - html

Trying to remove .html extensions from the site using .htaccess. So for example: www.mysite.com/charts.html would become www.mysite.com/charts
The following script is in the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
But when the url without the .html extension is entered in the browser, it shows a 403 Forbidden error. Any help would be appreciated.

I found this solution elsewhere:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
sources:
1) http://www.catswhocode.com/blog/10-useful-htaccess-snippets-to-have-in-your-toolbox
2) http://eisabainyo.net/weblog/2007/08/19/removing-file-extension-via-htaccess/

Related

Rewrite rules for .htaccess

I have a basic question about the .htaccess file.
Currently mine consists of:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
Redirect all links to the hypertext transfer protocol secure (https) of the website.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([^\.]+)$ $1.html [L]
Rewrite all .html files without their file extension.
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^/?(.+)/?$ /$1/index.html [L]
RewriteRule ^/?(.+/)index\.html$ /$1 [R=301]
Remove the index.html from site URLs. Should I remove the file extension (.html) because of the 2nd rule?
ErrorDocument 404 /404.html
Self-explanatory. Should I remove the file extension here as well?
And overall - is the code correct, is this the way it should look like in my .htaccess file?
I appreciate your help.
I ended up using this code:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([^\.]+)$ $1.html [L]
ErrorDocument 404 /404.html
The index.html remove from URLs rule was useless, because I changed my links in the html part:
Homepage
to:
Homepage
and analogical:
English Homepage
to:
English Homepage

Remove .html off URL. .htaccess

I'm building a site in a Node.js container, and the .htaccess is not working. I just want to remove the .html off the url, but the following just downloads the pages as files. Sorry if this has been answered, just need help. Thanks!
What we currently have:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
To remove .html all you need to use is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
EDIT:
Some hosting services require a slightly different layout:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Removing .html file extension from URL with an .htaccess file

I've tried a number of different snippets inside my .htaccess file in order to remove the .html file extension from displaying in the URL. So far I've read some other answers on here that haven't worked, researched some personal blogs, and even found some tricks that didn't work on Chris Coyier's CSS Tricks.
Here's a couple of the ones I've tried:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
As well as:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
I appreciate any help I can get.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

Permanent redirect after removing html extension in htaccess

I have these htaccess rules to remove html extension from urls:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
It works fine but I can still access from the URL with extension. How to make it so that when a user click the url with extension, will be redirected with a permanent (301) redirect to the extensionless url?
Thanks
Just add the little snippet at the end which indicates it is a 301 redirect.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [R=301,L]

Remove file extension with .htaccess: Error with trailing slash

I want to remove the file extension like .html from my websites with .htaccess. The final structure should be like so:
http://domain.com/file --> http://domain.com/file.html
http://domain.com/file/ --> http://domain.com/file.html
With my existing code in .htaccess I'll get "Internal Server Error" on my Browser when there's a trailing slash at the end. What can I do? Thanks!
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9-_]+)/?$ $1.html [L]
I suggest you to change your RewriteCond :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)(\.html){0}$ /$1.html [L]
EDIT : rule edited, I forgot infinite loop.