ogmo22
ogmo22

Reputation: 15

.htaccess file isn't removing the .html extension

Im quite new to this and would like to know why my code in the .htaccess file isn't removing the .html extension from each of the links in my nav bar, the live website can be seen at www.tekmillion.com

.htaccess code below:

RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [NC,L]

Thanks for any help.

Upvotes: 1

Views: 49

Answers (1)

Panama Jack
Panama Jack

Reputation: 24448

I think your problem lies in that the expectation is that you have links using .html in your nav bar is supposed to redirect to friendly url. That's not how this rule works.

Your current rule allows you to use this type of URL http://example.com/hello in the browser and it will internally be rewritten to http://example.com/hello.html

RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [NC,L]

If you still have URL's like http://example.com/hello.htmlin your navbar, that rule will not do anything because the request is for /hello.html which is a real file.

So in order to redirect http://example.com/hello.html to the friendly version without html extension and to prevent use of .html, you need to add another rule. This is the complete rule you need to use.

RewriteEngine On
RewriteBase /

#if someone enters URL with .html extension redirect to extensionless version
RewriteRule %{THE_REQUEST} ^[A-Z]{3,}\s/(.+)\.html
RewriteRule ^ %1? [R=302,L]

#internally rewrite extensionless URL to .html file
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [NC,L]

Let me know how this works out for you.

Upvotes: 1

Related Questions