: Blog/index.php/author/ how to create this kind of a URL for my php page? WordPress blog URL has this notation. chapter .php there is another path. it is not an actual existing directory on
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?
More posts by @Berryessa370
1 Comments
Sorted by latest first Latest Oldest Best
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>
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.