I've been hired to re-factor some homemade websites. Those websites are made in the good old fashion way: one page = one php file with header, content, script, footer. Now my client wants to update the layout of all the pages I feel like DRYing a bit the code by creating a clean URL system. Instead of :
example.com/page1.php, example.com/page2.php
I'd like to have:
example.com/page1, example.com/page2
Where the two pages call the same php file just charging the content of the page.
My questions are: 1. how to catch the URL and converts it into parameters? 2. how to avoid the website default behavior to search in directory /page1 or /page2?
If you are using the Apache web server, you can read-up on Multiviews. While normally used to serve up the same page in different languages, its effect is that whenever you specify a name without an extension, and that name isn't already a folder, then the file with the same name will be served.
For example, you ask for http://localhost/example
, and example
isn't a folder name, then if example.php
exists, it will be served up instead. You just have to make sure you don't have two or more files with the same name and different extensions, or a folder and file with the same name.