如果当前页面 URL 等于 X 或 X? [英] If current page URL equals X OR X?
问题描述
我在internetz上找到了这段代码,它检查当前页面的url;
I found this code on the internetz, it checks the current page url;
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
所以现在我可以做这样的事情了;
So now I can do something like this;
elseif (curPageURL() == "http://www.example.com/pageexample") {
<meta tags here>
}
太好了.但我也想将它用于分页页面.这些网址如下所示:
Great. But I would also like to use this for pagination pages. Those URLs look like this:
http://www.example.com/pageexample?start=30&groep=0
http://www.example.com/pageexample?start=60&groep=0
http://www.example.com/pageexample?start=90&groep=0
[....]
http://www.example.com/pageexample?start=270&groep=0
我可以对每个链接使用 if 语句......但我更愿意使用一个.是否可以添加通配符或其他内容?像这样我猜(注意 *
)
I could use a if statement for every of those links.. but I would much rather like to use one. Is it possible to add a wildcard or something? Like this I guess (notice the *
)
elseif (curPageURL() == "http://www.example.com/pageexample" OR curPageURL() == "http://www.example.com/pageexample?start=*&groep=0") {
edit:我想对所有这些 URL 执行此操作,因为我想为它们提供相同的 meta description
、
和 .我可以通过为每个页面 (10+ atm) 执行 if 语句来手动执行此操作,但我认为有更好的方法.
edit: I would like to do this for all those URLs because I want to give them the same meta description
, <title>
and <link rel="canonical"
. I could do this manually by doing an if-statement for every page (10+ atm) but I figured there was a better way.
推荐答案
听起来很像正则表达式问题:
Sounds a lot like regex problem:
if (preg_match("#^http://www.example.com/pageexample(\?start=[^&]*&groep=0)?#", curPageURL())) {
// it matches
}
表达式 [^&]*
就像你的 *
.;要匹配非空项目,请使用
[^&]+`.它匹配这些:
The expression [^&]*
acts like your *
.; to match non-empty items, use
[^&]+`. It matches these:
http://www.example.com/pageexample
http://www.example.com/pageexample?start=30&groep=0
更新
不清楚为什么需要与完整的规范 URL 进行比较,除非您有多个域指向相同的代码库.
It's not entirely clear why you need to compare against the full canonical URL, unless you have multiple domains point to the same code base.
这篇关于如果当前页面 URL 等于 X 或 X?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!