Reputation: 527
I'm trying to write a regex expression in PHP that will load content in all locations specified, or every location except those specified.
#load content everywhere EXCEPT locations expressed
<?php if (preg_match('/\/(contact\/|news\/|index\.html)/', $_SERVER['REQUEST_URI']) === 0): ?>
<div class="greeting">Bonjour</div>
<?php endif; ?>
#load content on locations expressed
<?php if (preg_match('/\/(contact\/|news\/|index\.html)/', $_SERVER['REQUEST_URI']) === 1): ?>
<div class="greeting">Konichiwa</div>
<?php endif; ?>
The problem I'm having is that I can't get the root of my website /index.html
(specifically http://example.com/index.html, nowhere else) to work alongside the wildcards for any page in /contact/
or /news/
.
To summarize again:
/contact/*
(any page in contact), /news/*
(any page in /news/), /index.html
(root of website)
I've tried a few different ways of adding the slash before index.html, but it either didn't work or returned a PHP error.
Upvotes: 1
Views: 723
Reputation: 786146
Make sure you use anchor ^
(line start) make sure you match URI from root:
if (preg_match('#^/(contact/|news/|index\.html|$)#', $_SERVER['REQUEST_URI']))
I also changed regex delimiter to #
so that you can avoid escaping /
in your regex.
Upvotes: 1