I am working with PHP5.3.6 on Windows 2008R2.
In WordPress, I can set all kinds of friendly URLs which ultimately route to one PHP page. From the outside, it looks like many pages, but is only one.
In the web.config file (or .htaccess) file, there is only one instruction as opposed to having one entry per page/article.
This means that somewhere PHP is looking at the URL, comparing it to the database (where the article titles exist) and then routing transparently to the page.
All of this is transparent to the end user.
How is this accomplished in a non wordpress PHP site?
Here’s a related answer that touches on how to do that. In brief, you’ll want to check the
$_SERVER['REQUEST_URI']and just parse that.Here’s a simple example of parsing the request (MVC routers are usually configurable, and can route and match for many different URI structures):
If your format is something like
news/article-sligyou can do this (example code, there are less rigid ways to do this):At this point your PHP script knows how to interpret the request. If this were a full blown MVC application, the router would load the matching controller and pass the request data to it. If you’re just doing a simple single page script, loading some data, then your DB calls would follow.
If the request is invalid, then a simple
header()call can notify the browser:And any data output would be the content of your 404 page.