3

WordPress blog URL has this notation. chapter .php there is another path. it is not an actual existing directory on the server. my question is how they do that? can I do something like that on my simple PHP website?

3

blog/index.php/author/

This specific URL format, containing index.php (which does map to a physical file) uses the additional pathname information, ie. /author/ to route the request. It does not require any additional URL rewriting by the server.

All requests are handled by the file /blog/index.php (as stated in the URL itself). Within index.php you can then reference the pathname information using the PHP superglobal $_SERVER['PATH_INFO'] to decide what you are going to do with the request. In the case of your example URL, $_SERVER['PATH_INFO'] holds the string /author/ (as mentioned above).

This does require PATH INFO being enabled on the server for it to work (otherwise, on Apache, it would just trigger a system generated 404). Path info is enabled by default for the PHP handler.

can I do something like that on my simple PHP website?

Yes. However, how easy that would be to implement on an existing site and whether that would result in an improvement over the existing URL structure is another matter.

If you have, what is really just a static site, where you are serving your files directly (ie. URLs map directly to filesystem paths, eg. /blog/author.php) then implementing this structure could require some restructuring with little to no advantage to the URL structure itself.

Note that this is different to a URL of the form /blog/author/ (not the absence of index.php). This would require some additional URL rewriting on the server.

When would you (normally) use this type of URL?

When you want to route all requests through a "front controller" (eg. index.php) and you don't have access to the servers rewrite engine. The content might be stored in a database of some kind and your "front controller" knows how to construct the response for any given request URL.

For example, in index.php you could have something like:

<?php
$content = [
    'STATUS' => 200,
    'TITLE' => null,
    'BODY' => null,
];
$url = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : null;
if ($url == '/author/') {
    // Lookup content from database or other file etc....
    $content['TITLE'] = 'Author';
    $content['BODY'] = '<p>My name is bob.</p>';
}
elseif ($url == '/contact/') {
    // Lookup content from database or other file etc...
    $content['TITLE'] = 'Contact Me';
    $content['BODY'] = '<p>Please complete the form below...</p>';
}
else {
    $content['STATUS'] = 404;
    $content['TITLE'] = '404 Not Found';
    $content['BODY'] = 'Sorry, could not find the page you requested.';
}
http_response_code($content['STATUS']);
?>
<html>
<head>
    <title><?={$content['TITLE']}?></title>
</head>
<body>
<?={$content['BODY']}?>
</body>
</html>
  • 1
    thank you sir this is a great answer. you solved the problem. – Vinoli Senanayake Dec 23 '17 at 5:19

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.