How come some site urls do not include a file extension?

后端 未结 13 1524
终归单人心
终归单人心 2020-11-28 09:38

I was browsing the internet and noticed, YouTube, for example, contains a URL like this to denote a video page: http://www.youtube.com/watch?v=gwS1tGLB0vc.

相关标签:
13条回答
  • 2020-11-28 10:16

    What you are seeing is an example of URL routing. Instead of pointing to a specific file (e.g. page.php), the server is using a routing table or configuration that directs the request to a handler that actually renders the html (or anything else depending on the mime type returned). If you notice, StackOverflow uses the same mechanism.

    0 讨论(0)
  • 2020-11-28 10:17

    below it what I use in my .htaccess to make the url still run correctly without the HTML or PHP extension.

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.html -f
    

    means that if the file with the specified name in the browser doesn't matching with directory (-d) or files(-f) in your webserver, then rewrite rule below

    RewriteRule ^(.*)$ $1.html
    

    i'm not sure how the below work but I think that after it rewrite with html and if it still not matching it then rewrite with php

    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
    

    if it still not matching it will be show 404 page.

    you also can redirect 404 with the code below in .htaccess

    ErrorDocument 404 /404.html
    

    importance is the code is working in for my site.

    http://mintnet.net/services

    http://php.mintnet.net/home

    those doesn't need the file-extension.

    0 讨论(0)
  • 2020-11-28 10:18

    File extensions are not used because of the idea that URIs (and therefore URLs) should be independent of implementation - if you want to access George W. Bush's addresses, you should be able to go to http://www.whitehouse.gov/presidents/georgewbush/addresses (for example). Whether the White House's servers are using PHP or Python or Perl doesn't matter to the end-user, so they shouldn't see it. The end-user doesn't care how the page was generated, because all web languages output the same HTML, CSS, and the like, and they're just viewing the page in their web browser.

    Most web frameworks build this functionality in by default, precisely for this reason, and it can be accomplished regardless with URL rewriting in most webservers. This ideal is codified in the W3C Style Guide, which is undoubtedly a big proponent in this idea being so widely accepted. It's outlined in their guide, "Cool URIs Don't Change", which should clear things up if you still don't quite understand the reasoning here. That document is the go-to statement on the issue, and the de facto standard for frameworks.

    It is worth noting that usually files that end up being downloaded (and sometimes data files used in AJAX) will still have their file extensions intact - http://example.com/song.mp3 or http://example.com/whitepaper.pdf - because they are intended to be saved to the end-user's computer, where file extensions matter. The extensions are not included for pages that are simply displayed - which is most pages.

    0 讨论(0)
  • 2020-11-28 10:19

    "www.youtube.com/watch" is a directory of YouTube. So it can basically be written as "www.youtube.com/watch/" with the ending forward slash.

    0 讨论(0)
  • 2020-11-28 10:24

    The key is the HTTP response header's Content-Type field. Something like that:

    HTTP 200 OK
    Content-Type: video/flv
    Content-Length: 102345
    
    DATA-DATA-DATA-DATA-DATA-DATA-....
    

    See also:

    Content-Disposition: attachment; filename=genome.jpeg;
         modification-date="Wed, 12 Feb 1997 16:29:51 -0500";
    

    More details: http://en.wikipedia.org/wiki/MIME

    0 讨论(0)
  • 2020-11-28 10:24

    Rule: File extensions should not be included in URIs

    On the Web, the period (.) character is commonly used to separate the file name and extension portions of a URI. A REST API should not include artificial file extensions in URIs to indicate the format of a message’s entity body. Instead, they should rely on the media type, as communicated through the Content-Type header, to determine how to process the body’s content.

    (1)http://api.college.restapi.org/students/3248234/transcripts/2005/fall.json (2)http://api.college.restapi.org/students/3248234/transcripts/2005/fall

    (1)File extensions should not be used to indicate format preference. (2)REST API clients should be encouraged to utilize HTTP’s provided format selection mechanism, the Accept request header. references: design REST api rulebook

    0 讨论(0)
提交回复
热议问题