【问题标题】:Check if url contains a word检查 url 是否包含单词
【发布时间】:2016-04-26 23:39:42
【问题描述】:

如何检查网址 - http://www.hotek.com.ua/live/5605/forum15000/223http://www.hotek.com.ua/5635/forum12200/223 是否包含“live”和“forum”字样。

我正在尝试这个(就像这里的Check if url contains certain word then display)但它没有帮助。

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

if ((strpos($url, 'forum') !== false) or (strpos($url, 'live') !== false)){

 header('HTTP/1.0 404 Not Found');

 echo "404 Not Found";

 exit();  
}

我需要 404 Not Found 或 noindex 或 301 永久移动到主页只是为了从 Google 索引中删除这些页面。

【问题讨论】:

  • 只是你应该在你的情况下使用&&而不是||
  • 究竟是什么不起作用?您说这无济于事,但不是问题到底是什么,即您所期望的以及发生了什么。

标签: php url


【解决方案1】:

试试这个:

strpos - 返回针头相对于 haystack 字符串开头的位置(与偏移量无关)。还 请注意,字符串位置从 0 开始,而不是 1。

$str1 = "http://www.hotek.com.ua/live/5605/forum15000/223";
$str2 = "http://www.hotek.com.ua/5635/forum12200/223";

if(strpos($str1, "live") > 0 || strpos($str1, "forum") > 0 ){
    header('HTTP/1.0 404 Not Found');
    echo "404 Not Found";
    exit();  
}

【讨论】:

  • 感谢所有回答。
【解决方案2】:

你可以试试:

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if (preg_match('/forum|live/i', $actual_link)) {
    header('HTTP/1.0 404 Not Found');
    //you cannot echo anything else after header or you'll get an error
    exit();
}

我需要 404 Not Found 或 noindex 或 301 永久移动到主 页面只是为了从 Google 索引中删除这些页面。

您也可以通过以下方式要求谷歌删除网址:

https://www.google.com/webmasters/tools/url-removal


参考文献:

https://support.google.com/webmasters/answer/1663419?hl=en

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 2017-11-10
    • 2014-01-25
    • 2016-08-19
    • 2011-10-28
    • 2020-07-06
    • 2012-03-26
    相关资源
    最近更新 更多