Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

HTML

Mark Lawson
Mark Lawson
6,148 Points

Removing .html from url

When we save html docs they have a .html at the end. When I upload my site and then go to each page the url shows the page with the .html showing. I want to remove the .html.

Example: Your site shows- https://teamtreehouse.com/community

If this was my site it would show- https://teamtreehouse.com/community.html

How do I remove the html?

Thanks

Mark

6 Answers

You need to create a file called '.htaccess' in the root directory of your site containing the following code:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

And then make sure all the links on your site don't contain '.html' at the end, e.g.:

<a href="/about.html">

should be replaced with:

<a href="/about">

Hope this helps! If you need more help with the .htaccess file, google has a ton of results on it

Mark Lawson
Mark Lawson
6,148 Points

Thank you, where do I place the ht access file? How do I make an ht access file.

Regards

Mark

You just need to create a new file using your text editor the same way you would any other html or css file, but save it as simply '.htaccess'

And save it into the root directory, so the same folder that you have your index.html file.

Mark Lawson
Mark Lawson
6,148 Points

Thanks, I will give it a go.

Mark Lawson
Mark Lawson
6,148 Points

OK, I have copied and pasted your code into a code editor and saved it as htaccess.txt; not sure if this is right; however, it has not worked. I am hoping at this point you can tell me where I have gone wrong.

Thanks

Mark

Oh all you need to do is change the name of the file.

Instead of 'htaccess.txt' it should be '.htaccess'

So .htaccess is the file extension but it has no name

I hope that makes sense

Mark Lawson
Mark Lawson
6,148 Points

You are a star!! The only, and best answer by far.

Haha no problem I'm glad I could help!

Mark Lawson
Mark Lawson
6,148 Points

I have voted you best answer. Thank you