1

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.

1
  • yeah it seems to be okay in my end as well Commented Jan 15, 2016 at 1:59

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.