3

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?

Ralph M. Rivera
  • 769
  • 3
  • 12
  • 25

3 Answers3

1

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-slig you can do this (example code, there are less rigid ways to do this):

list($section, $slug) = explode('/', trim($_SERVER['REQUEST_URI'], '/'));

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:

header('HTTP/1.0 404 Not Found');

And any data output would be the content of your 404 page.

Community
  • 1
  • 1
Tim Lytle
  • 17,549
  • 10
  • 60
  • 91
  • Awesome. Thanks. Don't I then need to issue a 200 for a success or a 404 for a page not found? If that is the case, How do I send that to the browser? – Ralph M. Rivera Nov 18 '11 at 17:01
1

I can't vouch for wordpress, one method I have used is to redirect 404's in .htaccess to index.php and then have that file sort by parsing:

$sub = $_SERVER['SERVER_NAME']; 
$file = $_SERVER['REQUEST_URI'];

$sub can be used to mask non existant subdomains to a specific file. $file can be used in a switch or if clause to include / redirect based on file name.

Obviously, you need to make sure that the alias' are not actual files in your doc root.

David Barker
  • 14,484
  • 3
  • 48
  • 77
0

Its called Routing(you can also check info about Front Controller pattern). You can write your own implementation by redirecting all your requests to single file using server settings and parse request in this file. You can also check, for example, Zend_Controller_Router docs and sources to understand how it works.

http://framework.zend.com/manual/en/zend.controller.router.html

http://framework.zend.com/manual/en/zend.controller.html

selfsx
  • 581
  • 7
  • 23