【问题标题】:Regarding a SEO Search and html characters关于 SEO 搜索和 html 字符
【发布时间】:2014-06-07 08:53:43
【问题描述】:

我正在开发一个允许用户搜索站点数据库的站点,搜索脚本是自定义的,它是在 SQL 中使用“匹配”的简单案例。

工作流程如下:

用户输入文本(可以是文本+数字+一些特殊符号)。 表单被提交到一个页面,该页面将生成一个 SEO URL (http://www.example.com/search/URLENCODE(SEARCHTERM))。

现在我遇到的问题是,有些字符显示在 slug 中,有些则没有。我做错了什么?

代码:

在搜索重定向页面中:

$title=rawurldecode($_GET['searchtext']);
$title=ucfirst(mb_strtolower($title,'UTF-8'));
$title=preg_replace('/[^-\pL.\s0-9- ?\'"+:()<>&]/u', '', $title); //replaces everything else apart from characters and symbols mentioned above.
$title=htmlspecialchars(trim(html_entity_decode($title)));
$slug=rawurlencode($title);
$link="http://www.example.com/search/".$slug;

在搜索页面中它为:

$title=rawurldecode($_GET['searchtext']);
$title=preg_replace('/[^-\pL.\s0-9- ?\'"+:()<>&;]/u', '', $title);
$title=str_replace("%09","",$title);
$title=str_replace("%20"," ",$title);
$title=str_replace("-"," ",$title);
$title=trim($title,' ');
$title=trim($title,'%20');
$title=trim($title,'-');
$title=trim($title,' ');
$title=html_entity_decode($title);

然后 SELECT id,Title from table match(title) against ('$title');

以上方法适用于:

  • UTF 字符串。
  • 仅包含字符的字符串。
  • 像“Title ;)”和“Title :-)”这样的字符串

问题是,它不适用于某些以转换形式具有“&”的字符,例如 、& 符号等。

是什么让它出错了? 请指导。

【问题讨论】:

  • URL 中有特定含义的字符。您可能希望修改那些由您的编码解释的内容,以便在您取回 URL 时它们不会弄乱您的 URL。
  • @Fluffeh,你能告诉我我做错了什么......或者至少我能做些什么来让这些东西按我的意愿工作。基本上,我想要的只是用户输入一个单词(用字符)和一些允许的符号。搜索应该是安全的,没有 xss/sql 注入(尽管我使用的是 HTML Purifier+Prepared Statements)。最后,在安全之后,我希望 URL slug 对 SEO 来说是安全的。
  • @Cyber​​Wizard:首先要了解“你的”问题。您特别在 URI 中寻找 保留字符。了解它是如何工作的,然后决定你想用它做什么。并且不要被营销所迷惑:任何正确编写的 URI 对于 SEO 来说都是安全的。

标签: php utf-8 uri slug


【解决方案1】:

是什么原因造成的?

您的期望。

请指导。

https://www.rfc-editor.org/rfc/rfc3986 - 在您的情况下,一个不错的条目可能是 2.2. Reserved Characters

【讨论】:

猜你喜欢
  • 2012-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 2011-03-11
  • 1970-01-01
相关资源
最近更新 更多