0

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

razz
  • 41
  • 1
  • 9
  • I'm not sure of your server environment, but if you are using Apache and enable the rewrite module, you could do it that way. – Progrock Sep 13 '16 at 22:16
  • Sorry forgot to mention it. Its running on nginx. The code i provided does work perfectly but only on /categories and not /categories/AnythingElse. – razz Sep 13 '16 at 22:18
  • You can use rewrite rules under nginx: https://www.nginx.com/blog/creating-nginx-rewrite-rules/ – Progrock Sep 13 '16 at 22:28
  • You could use a regex or partial string comparison. See https://stackoverflow.com/questions/4366730/how-to-check-if-a-string-contains-a-specific-word-in-php – Robert Sep 13 '16 at 22:44
  • Note: `'\categories'` is a different string to `'\categories\'`. – Progrock Sep 14 '16 at 09:44

2 Answers2

1

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/'
) {}
Progrock
  • 7,373
  • 1
  • 19
  • 25
  • For some reason this shows the html on all pages all across the website. ps. "foo" in here `$request_uri = '/categories/foo';` could be anything which i don't know, a digit, a letter, maybe combined. – razz Sep 13 '16 at 23:06
  • One assumes you have `$request_uri = $_SERVER['REQUEST_URI']`, instead of the fixed value given above. You'll also want to exit after the include. – Progrock Sep 14 '16 at 09:18
0

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';
}
?>
Charli W.
  • 71
  • 1
  • 4
  • But in my case, your answer is the only one that works. Thanks everyone for the help. – razz Sep 14 '16 at 09:07
  • You might want to anchor that regex pattern to the beginning of the string: `"/^\/categories\/.*/"` – Progrock Sep 14 '16 at 09:40