将通配符多个网址重定向到主页

时间:2016-03-13 13:19:13

标签: php .htaccess

我想使用PHP或htaccess将未知网址重定向到我的主页。找到一个,但似乎只是一个通配符匹配。我想知道使用条件标签或开关是否可以帮助我将与其特定字词匹配的未知网址重定向到主页。

这是个主意。

if ("unknown url 1" is equal to "wildcard url condition") :
 - location redirect to "domain.com"
elseif ("unknown url 2" is equal to "wildcard url condition") :
 - location redirect to "domain.com"
endif;

虽然,如果有另一种选择,并且使用htaccess轻松重定向,希望有人可以帮助我如何编写它。

我希望在可能的情况下使用此功能的原因是为了减少向我们的网站站长工具注册404 Not Found的数量。

更新

这是我正在研究的另一个想法。

// List all keywords for wildcard match
$patterns = array( 'dev', 'new', 'php', 'html' );
$patterns_flattened = implode(' ', $patterns);

// Get 404 Not Found URL
$404_notfound = $_SERVER['REQUEST_URI'];

// Match the define keywords and 404 Not Found
if ( preg_match('/'. $patterns_flattened .'/', $404_notfound) ) :
  header("Location: http://www.domain.com");
endif;

虽然我在这里收到错误......任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

好的,我想我找到了另一种解决方案。

$wildcards['keywords'] = array('dev', 'new', 'php', 'html');
$string = $_SERVER['REQUEST_URI'];

$max = array(null, 0);
foreach($wildcards as $k => $v) {
  $replaced = str_replace($v, '##########', $string);
  if (preg_match_all('/##########/i', $replaced, $matches)) {
      header('Location: http://www.domain.com/');
      exit;
  }
}
相关问题