我建议您仔细查看 SO 上这个问题的漂亮 URL 并获得一些想法。不用说,SO 问题在 Google 搜索结果中的排名非常高。因此,从 SO URL 约定中获取线索,我建议您使用以下 3 种漂亮的 URL 格式:
- /articles/89/mytitle为
/articles_en.php?artid=89
- /halls/65/sometitle for
/halls.php?fairid=65
- /companies/65-23/company为
/companies.php?fairid=65&hallid=23
现在创建 3个查找表articles
,如下所示:halls
companies
表:文章:
+-------+-------+
| artid | title |
+-------+-------+
桌厅:
+--------+-------+
| fairid | title |
+--------+-------+
表公司:
+--------+--------+------+
| fairid | hallid | name |
+--------+--------+------+
现在对于以上 3 个漂亮的 URL 处理,将此代码添加到您的 .htaccess 下$DOCUMENT_ROOT
:
RewriteCond %{QUERY_STRING} ^artid=\d+$ [NC]
RewriteRule ^articles_en\.php/?$ router.php?handler=article [L,NC,QSA]
RewriteRule ^articles/(\d+)/?(.*)$ router.php?handler=article&artid=$1&title=$2 [L,NC,QSA]
RewriteCond %{QUERY_STRING} ^fairid=\d+$ [NC]
RewriteRule ^halls\.php/?$ router.php?handler=hall [L,NC,QSA]
RewriteRule ^halls/(\d+)/?(.*)$ router.php?handler=hall&fairid=$1&title=$2 [L,NC,QSA]
RewriteCond %{QUERY_STRING} ^fairid=\d+&hallid=\d+$ [NC]
RewriteRule ^companies\.php/?$ router.php?handler=company [L,NC,QSA]
RewriteRule ^companies/(\d+)-(\d+)/?(.*)$ router.php?handler=company&fairid=$1&hallid=$2&name=$3 [L,NC,QSA]
最后有你的router.php
代码是这样的:(示例代码)
<?php
// TODO: Add sanitization checks for presence of required parameters e.g. handler and lookup failures
$handler = mysql_real_escape_string($_GET['handler']);
switch ($handler) {
case 'article':
$artid = mysql_real_escape_string($_GET['artid']);
$title = mysql_real_escape_string($_GET['title']);
if (empty($title)) {
#header("HTTP/1.1 301 Moved Permanently");
header("Location: /articles/$artid/" . lookupArticle($artid));
exit;
}
else
require_once("articles_en.php");
break;
case 'hall':
$fairid = mysql_real_escape_string($_GET['fairid']);
$title = mysql_real_escape_string($_GET['title']);
if (empty($title)) {
#header("HTTP/1.1 301 Moved Permanently");
header("Location: /halls/$fairid/" . lookupHall($fairid));
exit;
}
else
require_once("halls.php");
break;
case 'company':
$fairid = mysql_real_escape_string($_GET['fairid']);
$hallid = mysql_real_escape_string($_GET['hallid']);
$name = mysql_real_escape_string($_GET['name']);
if (empty($name)) {
#header("HTTP/1.1 301 Moved Permanently");
header("Location: /companies/$fairid-$hallid/" . lookupCompany($fairid, $hallid));
exit;
}
else
require_once("companies.php");
break;
}
function lookupArticle($artid) {
// $title = mysql_result(mysql_query("SELECT title FROM articles WHERE artid=$artid"), 0, "title");
static $articles = array(89 => 'Title\'s A', 90 => 'Title, 1B', 91 => '@Article= C');
return normalize($articles[$artid]);
}
function lookupHall($fairid) {
// $title = mysql_result(mysql_query("SELECT title FROM halls WHERE fairid=$fairid"), 0, "title");
static $halls = array(65 => 'Hall+ A', 66 => 'Hall B', 67=> 'Hall C');
return normalize($halls[$fairid]);
}
function lookupCompany($fairid, $hallid) {
// $title = mysql_result(mysql_query("SELECT name FROM companies WHERE fairid=$fairid and hallid=$hallid"), 0, "name");
static $companies = array('65-23' => 'Company% A', '66-24' => 'Company B', '67-25' => '(Company) C');
return normalize($companies[$fairid .'-'. $hallid]);
}
function normalize($str) {
return preg_replace(array('#[^\pL\d\s]+#', '#\s+#'), array('', '-'), strtolower($str));
}
?>
一旦您确认它工作正常,取消注释301 Moved Permanently
行以获得更好的 SEO 结果。
PS:我使用normalize
PHP 函数来获取所有小写的 URL 文本,清除特殊字符并将所有空格转换为连字符。