【发布时间】:2010-12-22 15:19:08
【问题描述】:
我的网站是一个搜索引擎。
在我的 .htaccess 文件中:
RewriteRule ^news/(.*)\.html$ /results/news.php?name=$1
我的链接结构是/news/what-is-up.html,使用连字符。
当我搜索 /results/news.php?name=hello world 时,我得到的结果与我使用 /news/hello-world.html 时不同。 p>
似乎搜索是使用连字符而不是空格来搜索hello-world,我可以使用+ 符号轻松解决它,但我想使用连字符。有人知道怎么做吗?
这是我用于将 name 变量转换为 URL 友好的代码:
function toAscii($str, $replace=array(), $delimiter='-') {
if( !empty($replace) ) {
$str = str_replace((array)$replace, ' ', $str);
}
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
return $clean;
}
我的搜索引擎使用这样的 http 位置:
if ($q != '') {
header( 'Location: http://'.$_SERVER['SERVER_NAME'].'/news/'.toAscii($q).'.html' );
die();
}
else
{
header('Location:http://'.$_SERVER['SERVER_NAME'].'/news/');
die();
}
谢谢
【问题讨论】:
-
替换连字符的代码块在哪里?我认为这与您的重写规则无关。 /results/news.php?name=hello-world 应该产生与 /news/hello-world.html 相同的结果
-
关键字是 urldecode() 但仅适用于“+”号。
-
如果有人实际上在寻找
hello-world而不是hello world怎么办? -
我开始认为问题是在搜索中使用重定向...但我不知道如何以不同的方式进行:)
-
@Peibol:不,问题在于做这种不必要的“URL 友好”的事情。它掺假了用户输入,因此结果可能不是用户想要的。
标签: php apache url .htaccess mod-rewrite