多个目录和文件的php preg_match regex
我正在尝试用PHP编写一个正则表达式,它将在指定的所有位置或除指定位置之外的每个位置加载内容多个目录和文件的php preg_match regex,php,regex,Php,Regex,我正在尝试用PHP编写一个正则表达式,它将在指定的所有位置或除指定位置之外的每个位置加载内容 #load content everywhere EXCEPT locations expressed <?php if (preg_match('/\/(contact\/|news\/|index\.html)/', $_SERVER['REQUEST_URI']) === 0): ?> <div class="greeting">Bonjour</div> &l
#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; ?>
#除了所表示的位置之外,在任何地方加载内容
你好
#在表示的位置上加载内容
近一川
我遇到的问题是,我无法让我的网站/index.html
(特别是在其他任何地方)的根与/contact/
或/news/
中任何页面的通配符一起工作
再次总结一下:
/contact/*
(任何联系页面)、/news/*
(任何/news/)页面、/index.html
(网站根目录)
我尝试了几种在index.html之前添加斜杠的不同方法,但它要么不起作用,要么返回PHP错误。确保使用锚定
^
(行开始)确保与根目录中的URI匹配:
if (preg_match('#^/(contact/|news/|index\.html|$)#', $_SERVER['REQUEST_URI']))
我还将正则表达式分隔符更改为
#
,这样您就可以避免在正则表达式中转义/
。确保使用锚定^
(行开始)确保与根中的URI匹配:
if (preg_match('#^/(contact/|news/|index\.html|$)#', $_SERVER['REQUEST_URI']))
我还将正则表达式分隔符更改为
#
,这样您就可以避免在正则表达式中转义/
。很抱歉响应延迟。我试过了,但它似乎仍然没有意识到index.html是类似example.com/
或example.com
的根。与以前一样,它仍然适用于/contact/*
和/news/*
,但无论我做什么,它似乎都无法识别网站的根目录。测试时浏览器中的URL是什么?我尝试了example.com
,example.com/
。它适用于example.com/index.html
,但我认为在本例中,它可以在服务器端理解它,因为当您刚刚转到example.com
时,index.html是在根目录下加载的。我怎样才能在regex中定义域的首页而不是index.html
?好的,我做了一个编辑来处理URL中的example.com
。请查收。很抱歉延迟回复。我试过了,但它似乎仍然没有意识到index.html是类似example.com/
或example.com
的根。与以前一样,它仍然适用于/contact/*
和/news/*
,但无论我做什么,它似乎都无法识别网站的根目录。测试时浏览器中的URL是什么?我尝试了example.com
,example.com/
。它适用于example.com/index.html
,但我认为在本例中,它可以在服务器端理解它,因为当您刚刚转到example.com
时,index.html是在根目录下加载的。我怎样才能在regex中定义域的首页而不是index.html
?好的,我做了一个编辑来处理URL中的example.com
。请查收。