重定向URL中的坏/成人关键字


Redirect bad/adults keywords in URL

如何重定向URL中的多个坏/成人关键字?

我试着用htaccess做这件事,但我不认为它对许多关键词有好处

示例:

http://example.com/mp3/sex-with-me/page/1重定向到http://example.com/mp3/with-me/page/1

http://example.com/video/selena-gomes-porn/page/1重定向到http://example.com/video/selena-gomes/page/1

htaccess文件中的一些代码:

RewriteRule ^mp3/(.*)-sex/page/(.*)?$ http://site.com/mp3/$1/page/$2  [R=301,L] 
RewriteRule ^mp3/(.*)-sex-(.*)/page/(.*)?$ http://site.com/mp3/$1-$2/page/$3 [R=301,L] 
RewriteRule ^mp3/sex-(.*)/page/(.*)?$ http://site.com/mp3/$1/page/$2 [R=301,L]
RewriteRule ^video/(.*)-porn/page/(.*)?$ http://site.com/video/$1/page/$2  [R=301,L] 
RewriteRule ^video/(.*)-porn-(.*)/page/(.*)?$ http://site.com/video/$1-$2/page/$3 [R=301,L] 
RewriteRule ^video/porn-(.*)/page/(.*)?$ http://site.com/video/$1/page/$2 [R=301,L]

用PHP可以做到这一点吗?

在PHP中这样做是可能的,当然。。。我只是不确定这是个好主意。您可以将每个请求重定向到一个PHP页面,分解$_SERVER['REQUEST_URI'],运行一些正则表达式,然后继续到该页面。

我看到了一些大问题。首先,为什么要更改URI,但仍然允许访问该网站?如果这是您自己的网站,您应该在创建URI之前过滤这些单词。如果这应该是一个代理,那么为什么允许访问URI中有标记单词的网站呢?(尤其是因为有很多比基于URI的PHP过滤更好的方法来拒绝访问不合适的材料)

其次,你对米德尔塞克斯镇或运动员盖伊先生有什么看法?如果密西西比河沿岸的堤坝破裂怎么办?如果有人在写电子阅读器(Nook,即Nook),你也可能会遇到问题。我可以通过添加一些连字符或其他垃圾字符来绕过你的过滤器。基本上,基于URI内容的过滤是非常有问题的,而且不太可能工作得很好。

如果你想用PHP做这件事,它可能是这样的:

<?php
$uri_component = explode('/',$_SERVER['REQUEST_URI']);
foreach($uri_component as $fragment){
    if(preg_match('/regex/',$fragment) echo "BAD WORD";
}
?>

如果你的网站有入口点,例如通过index.php,你可以定义带有坏单词的数组,使用数组在url中搜索这个单词,如果发现坏单词,则重定向到正确的url。在index.php中:

<?php
$badWords = array('sex','prOn','etc');
$uriParts = explode('/',$_SERVER["REQUEST_URI"]);
// build preg with bad words
$preg = '/'.implode('|',$badWords).'/is';
foreach ($uriParts as $k=>$part)
{
  $uriParts[$k]=preg_replace($preg,'',$uriParts[$k]);
}
// if bad words were found
if($uri='/'.implode('/',$uriParts)!=$_SERVER["REQUEST_URI"])
{
  $newUrl = 'http://'.$_SERVER["SERVER_NAME"].$uri;
  // redirecting user to good url with http code 301
  header("HTTP/1.1 301 Moved Permanently");
  header('Location: '.$newUrl);
  exit();
}

如果您的网站有多个入口点,例如mp3.php、video.php等。。您可以将上面看到的代码保存在文件bad_words_guard.php中:),并将其包含在每个条目文件中:

<?php
require_once('path/to/bad_words_guard.php');
...