Reputation: 41
I'm nearly done with finding a way to show a .html file on certain pages only.
In this case i want test.html to be shown on http://www.example.com/categories/AnyPageThatExcistsInCategories
I figured out the following code works on /categories.
<?php if ($_SERVER['REQUEST_URI'] == '/categories/') { include 'test.html';} ?>
I only need the golden tip on how to get it also working on pages like /categories/ThisCanBeAnything and categories/ThisCanBeAnything/AndThisAlso etc etc server config is nginx.
thank you
Upvotes: 0
Views: 587
Reputation: 7485
You could see if the request uri begins with the string '/categories/':
<?php
$request_uri = '/categories/foo';
if (strpos($request_uri, '/categories/') === 0 )
{
include 'your.html';
}
Substitute the value of $request_uri above with $_SERVER['request_uri']
. Under the assumption that you have this logic in a front controller.
Further:
<?php
$request_uris = [
'/categories/foo',
'/categories/',
'/categories',
'/bar'
];
function is_category_path($request_uri) {
$match = false;
if (strpos($request_uri, '/categories/') === 0 )
{
$match = true;
}
return $match;
}
foreach ($request_uris as $request_uri) {
printf(
"%s does%s match a category path.\n",
$request_uri,
is_category_path($request_uri) ? '' : ' not'
);
}
Output:
/categories/foo does match a category path.
/categories/ does match a category path.
/categories does not match a category path.
/bar does not match a category path.
In use:
if(is_category_path($_SERVER['REQUEST_URI'])) {
include 'your.html';
exit;
}
You may want to not match the exact string '/categories/', if so you could adjust the conditional:
if(
strpos($request_uri, '/categories/') === 0
&& $request_uri !== '/categories/'
) {}
Upvotes: 1
Reputation: 71
Progrock's example will work just fine, but here is another example using a regex match instead of strpos, in case you're curious!
<?php
if (preg_match("/\/categories\/.*/", $_SERVER['REQUEST_URI'])) {
include 'test.html';
}
?>
Upvotes: 0