5

How do I hide my .html files for example when I go to www.mysite.com it redirects to www.mysite.com/index.html but it hides the /\index.html so it stays as www.mysite.com but is actually www.mysite.com/index.html . I don't really know how to explain but if you understand me can you please help, thanks.

Jakub Jankowski
  • 731
  • 2
  • 9
  • 20
meme
  • 179
  • 1
  • 2
  • 12

5 Answers5

3

An .htaccess file is a simple ASCII file that you create with a text editor like Notepad or TextMate. It provides a way to make configuration changes on a per-directory basis.

RewriteRule ^([^\.]+)$ $1.html [NC,L]

Source - How to remove .php, .html, .htm extensions with .htaccess

Shannon Young
  • 1,536
  • 1
  • 17
  • 26
3

Please write below code in your .htaccess file.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]

The .html will be removed from your URL using above code. Lets say your contact us page URL is www.mysite.com/contact.html so using above code it will be www.mysite.com/contact instead.

Rahul Patel
  • 5,248
  • 2
  • 14
  • 26
0

Try this, here we are setting directory index so we do not have to mention index.html in every call. and rewriting every html without extension.

DirectoryIndex index.html

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([\w-]+)$ $1.html [L]
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
0

This method does not use apache. It is just restructuring the code.

You can also try using creating different directory for routes. Such as for contact page you would have a folder name contact in root dir of your project. Inside that folder place the contact.html as index.html. This restructuring can help you splitting the code as per routes.

Link to contact page would be www.mysite.com/contact (browser will www.mysite.com/contact/index.html).

Nilesh
  • 41
  • 2
  • 5
0

If you're asking to edit the URL text on the client side in the index page only. You could replace the pathname via JavaScript on the HTML file with the code from this answer:

history.replaceState('data to be passed', 'Title of the page', 'theNameWithoutTheHTML');

The name should be changed for each .js of each page, or you can use a more generic script and reuse it:

history.replaceState('data to be passed', 'Title of the page', location.pathname.slice(0, -5)); //This will replace it with the same path, removing the ".html" at the end (take care if the extension is different sized)

In this case, that you're redirecting to the .html file, it shouldn't be any problem when user reloads the page (as it would try to load the page without the .html).

Community
  • 1
  • 1