Hide Page Extensions (Like StackOverflow)

前端 未结 6 1302
故里飘歌
故里飘歌 2020-12-07 04:42

I want to hide page extensions like stackoverflow does. How does the following work?

http://stackoverflow.com/tags/foo
http://stackoverflow.com/tags/bar
         


        
6条回答
  •  一生所求
    2020-12-07 05:12

    When a web server gets a request for a URL, it has to decide how to handle it. The classic method was to map the head of the URL to a directory in the file system, then let the rest of the URL navigate to a file in the filesystem. As a result, URLs had file extensions.

    But there's no need to do it that way, and most new web frameworks don't. They let the programmer define how to map a URL to code to run, so there's no need for file extensions, because there is no single file providing the response.

    In your example, there isn't a "tags" directory containing files "foo" and "bar". The "tags" URL is mapped to code that uses the rest of the URL ("foo" or "bar") as a parameter in a query against the database of tag data.

提交回复
热议问题