我几乎已经找到了只在某些页面上显示。html文件的方法。
在这种情况下,我希望test.html显示在http://www.example.com/categories/AnyPageThatExcistsInCategories
我发现下面的代码在/categories上工作。<?php if ($_SERVER['REQUEST_URI'] == '/categories/') { include 'test.html';} ?>
我只需要关于如何让它也在像/类别/ThisCanBeAnything和类别/ThisCanBeAnything/AndThisAlso等页面工作的黄金提示等服务器配置为nginx。
谢谢
您可以查看请求uri是否以字符串'/categories/'开头:
<?php
$request_uri = '/categories/foo';
if (strpos($request_uri, '/categories/') === 0 )
{
include 'your.html';
}
将上面$request_uri的值替换为$_SERVER['request_uri']
。假设在前端控制器中有此逻辑。
<?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'
);
}
输出:/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.
在使用:if(is_category_path($_SERVER['REQUEST_URI'])) {
include 'your.html';
exit;
}
您可能不希望匹配字符串'/categories/',如果是这样,您可以调整条件:
if(
strpos($request_uri, '/categories/') === 0
&& $request_uri !== '/categories/'
) {}
Progrock的示例将工作得很好,但这里是另一个使用正则表达式匹配而不是strpos的示例,以防您好奇!
<?php
if (preg_match("/'/categories'/.*/", $_SERVER['REQUEST_URI'])) {
include 'test.html';
}
?>