Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Page Extensions (Like StackOverflow)

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

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

I've seen a lot of sites that do this, but I still don't know how this is accomplished (I have a LAMP stack).

like image 292
sels520 Avatar asked Mar 28 '26 08:03

sels520


1 Answers

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.

like image 89
Ned Batchelder Avatar answered Mar 30 '26 10:03

Ned Batchelder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!