Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

Removing .html links from URL

Hello!

I need some pointers on how to removing the '.html' from my URL and where to input the code?

I understand its via a htaccess method but i find any brief google searches create more confusion for me.

Thanks in advance.

Nick

8 Answers

Steven Parker
Steven Parker
243,318 Points

You seem to be asking unrelated questions. "htaccess" is the name of a server configuration file, it has nothing to do with HTML coding.

For your first question, it's unclear what you are doing that would involve removing '.html' from a URL (it won't be a URL any more if you do). It's also unclear if this is to be done in the server or client. But it seems unlikely to be an HTML topic question.

Can you be a bit more explicit about what you're trying to do?

Thanks Steven,

What I'm trying to do is rename my URL from www.website.co.uk/page.html to www.website.co.uk/page. So I'm basically trying to remove the .html part.

However, when I've sourced anything online it refers to htaccess. So I'm not sure where to start really so I hope you or someone could help

Steven Parker
Steven Parker
243,318 Points

Are you doing this just to display it on the browser screen? Where is this URL coming from? Do you have any existing code you can share?

(Add a comment instead of an answer, or edit your original question.)

It's just to remove it from the browser. My current website is www.nickcade.co.uk so the html is within each page

Steven Parker
Steven Parker
243,318 Points

Looking at your site, I notice you manually removed the ".html" from the href associated with "advancing transplantation". I also noticed that that particular link does not work. This is what I was trying to warn you about the first time. Once you remove that, the URL is no longer valid.

I'm confused by why you would think you want to do this anyway, since the URL isn't actually visible on the page.

I also noticed that your opening anchor tags are incomplete (they don't have the > character at the end). And the closing anchor tags should probably go after the closing article tags, to make the entire article area a link.

I'm not sure what those lines starting with "Rewrite..." are. But they are certainly not HTML (thus they just get displayed on the page).

The website is a work in progress at the moment so I was trying to work out a method of removing the .html tags from the browser/URL. Hence those random faults with the code were a case of trial and error.

I've cleared those errors. So are you seeing my point regarding having .html at the end of the browser?

Just to clarify the 'rewrite' stuff was what I found online and I was unsure where to input this code. Clearly it's not relevant.

I'm new to coding so I'm just trying to figure out how to clean these errors before I move forward with the site.

Thanks again. Hope you can still help.

Steven Parker
Steven Parker
243,318 Points

It looks like you missed my comment about fixing the anchor (<a>) tags. Here's an example of the first item with the fixes applied:

<a href="advancing-transplantation.html">  <!-- notice the closing angle of the opening tag here -->
  <article class="thumbnails">
    <div class="thumb">
      <img src="img/adv_trans_thumbnail_300x170.png" alt="adv_trans_thumbnail_300x170" width="300" height="170">
      <span class="overlay">
            </span> </div>
    <div class="info">
      <h1>Advancing Transplantation </h1>
      <p>Event Branding </p>
  </article>
</a>  <!-- notice the closing tag moved to here -->

I'm still confused about why you'd want to remove the ".html" from the links, considering that it would break them and that they are not visible on the page. So no, I'm not seeing the point. Can you explain this?

(I'll be away for a while but I'll check back in a few hours)

I don't want to remove the links, its just hiding them from the browser so they're not visible. I guess in a nutshell its a case of renaming the each page, so it reads www.nickcade.co.uk/advancing-transplantation and not www.nickcade.co.uk/advancing-transplantation.html

So its that '.html' part at the end that i'd like to hide in some way.

Thanks for the anchor tag point, i'll look into tidying these up a little later.

Steven Parker
Steven Parker
243,318 Points

I still don't know what you mean by "hiding them from the browser so they're not visible." As far as I can see, they are not visible.

Or do you mean after you go to the link, and see the URL in the browser's address window? Like this forum page ends in "removing-html-links-from-url" with no ".html" after it? But that's because this page is not stored as HTML It's being constructed in the server and the URL doesn't point to a file but to a routing function in the controller.

That stuff about .htaccess is beginning to make sense now. You can simulate routing behavior using .htaccess, with a line like this (this line goes in the .htaccess configuration file on the server's file system, not in your code):

redirect /advancing-transplantation http://www.nickcade.co.uk/advancing-transplantation.html

You'd need a line like that for every link you want to redirect, then you'd change the href's in your links. But I don't see any value in it, it increases the maintenance overhead of your site, and it slows down response times. And while I can't be sure without trying it, I suspect the actual URL would still be visible after redirection in the browser's address box.

That part of the URL is vital because its shows which is in use, you could never remove that. Well, the only way would be to save the file with no extension, but then the file could not be used as your computer wouldn't know how to use it. That last part is the telling the browser what kind of file it is and how to use it. The .html extension tells the computer that it is and HTML file and that the code needs to be executed in that way.

Hope this answer helps.