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.
-
You want to remove .html from all URL. Lets say for contact us page is should be www.mysite.com/contact instead of www.mysite.com/contact.html? – Rahul Patel Oct 04 '16 at 10:47
-
This is not really a javascript problem. Focus on URL rewriting for apache. – Steeve Pitis Oct 04 '16 at 10:49
-
@RahulPatel yes thats correct, how would i do that? – meme Oct 04 '16 at 10:49
-
Apache automatically forwards all requests to `/` to `/index.html`. Why do you want to make the URL explicit? – Halcyon Oct 04 '16 at 10:56
5 Answers
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

- 1,536
- 1
- 17
- 26
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.

- 5,248
- 2
- 14
- 26
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]

- 7,426
- 10
- 37
- 45
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).

- 41
- 2
- 5
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).

- 1
- 1