1

By my limited understanding the " / " in an URL defines a path in the file directory the html is suppost to be pulled from.

So how is it possible that sites like reddit have URLs such as "reddit.com/u/username", where the slash doesn't define the file directory but is more so taken as a query that requests certain data from a user? Shouldn't you get an error back, that the site doesn't exist because it's not actually a file in the directory?

  • 1
    You mean something like https://stackoverflow.com/questions/25080835/pretty-urls-with-htaccess – Nigel Ren Dec 30 '21 at 10:52
  • How do you know that `/u/` isn't a file directory? – brombeer Dec 30 '21 at 10:54
  • 1
    That happens in an outer tier. You configure your web server to process requests any way you see fit. Mapping to physical files and directories on server disk is just one of the options. – Álvaro González Dec 30 '21 at 10:55
  • Many frameworks and sites today uses the [front controller pattern](https://en.wikipedia.org/wiki/Front_controller) together with a router, in some form. – M. Eriksson Dec 30 '21 at 11:20

2 Answers2

1

By my limited understanding the " / " in an URL defines a path in the file directory the html is suppost to be pulled from.

No. It defines a path segment in the path requested from the HTTP server.

The HTTP server is software, it can be written to handle that path in any way the developer likes.

"Map the URL path onto a filesystem path" is only one (simple and common) way an HTTP server can handle paths in URLs.

"Pass it on to a server side program which reads the whole URL and acts in a custom way" is another. If you were using PHP then you might read $_SERVER['REQUEST_URI'].

"Convert the path segments into query string parameters on a different URL and effectively start the request handling again" is another. Typically, if you were using Apache HTTPD, you would use mod_rewrite for this.

"Have a custom webserver which maps them directly to functions" is another. It's a common approach when writing server side code in Node.js (e.g. with Express routes).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Beacuse they use rewrite for pretty urls.

  • mod_rewrite is one way to achieve that for one specific web server based on the when the programming framework uses query strings. There are many other ways. – Quentin Dec 30 '21 at 12:04