Tell me more ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I know HTML and CSS like a mid level still not a pro. Many times I see in URL extensions are hidden (e.g. .html, .php) but everybody is not doing this.

Why do they do that? Is it for security, or for the URL to look cleaner, or for some other purpose?

share|improve this question
Oops, sorry I miss-read. – bybe May 9 at 19:19

3 Answers

up vote 20 down vote accepted

There are several reasons to remove extensions from URLs

  • To make the URLs look cleaner.
  • To make URLs easier to type.
  • To make URLs easier to remember.
  • To make URLs more SEO keyword friendly
  • To be able to change technologies -- If you ever want to move your site from one technology to the other, its easiest to do so without users even knowing if there are no extensions on the URLs.

Keep in mind that many sites are generated by a content managment system (CMS) that would make URLs look like this: /index.php?page=this-is-the-widget-page That is particularly ugly. It has far more cruft than just an extension. Rewriting to remove index.php?page= makes it much better.

Extensions are not needed on the web because servers send the type of document as a header. Web pages are served as text/html, images as image/png or image/jpeg. This lets browsers know how to render the content without using an extension to figure out that the url contains text, html, pdf, or image. (For more information see the Wikipedia article Internet media type.)

Some webmasters choose to use an extension on their URLs that matches this content type. So any text/html document would have a .html extension and any image/png document would have a .png extension. That can help when the URLs are saved to the file system where the meta data about their content type is lost. In most file systems, the program that opens the file is chosen by the extension. So even if a page is served by PHP, some webmasters remove the .php extension, and some replace it by .html.

There is also the question as to whether URLs might be better ending in trailing slash (/) when they have no extension which has a lot of discussion on StackOverflow.

share|improve this answer
4  
In some frameworks (e.g. MVC) the URL doesn't identify a file, so there is no extension to hide. – Henrik Ripa May 10 at 8:31
Good, thorough answer, +1 – Patrik Alienus May 13 at 9:34

All Web servers have one or more "default files". It's the file that will be displayed whenever a visitor goes to a URL that ends in a slash '/', i.e. a folder.

If the default file name on your web server is index.php and a visitor goes to www.example.com/pagename/, they are actually accessing www.example.com/pagename/index.php

If there is no trailing /, the web server is probably just re-writing the URL to remove it, since it's not necessary. This site, in fact, does that.

share|improve this answer
1  
I'm not sure the question was "how" but rather "why". Patrik your answer is absolutely correct regarding the how. Stephen however, answered the "why" – Brandt Solovij May 9 at 20:02
Oh right you are, my bad :) – Patrik Alienus May 13 at 9:32

Totally agree as explain by "Stephen Ostermiller" but I would like to mentioned the trick behind that to hide extension of URLs. And for that you have to use .htaccess rewrite rule, here is the script that help you out:

Redirect external .php requests to extensionless URL

RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.