3

我有 php 应用程序,由于客户要求有 SEO 友好的 url,因此必须部分重写。

我的链接如下:

www.mysite.com/articles_en.php?artid=89,我将不得不在其中更改网址:

www.mysite.com/articleTitle

然后我有这个网址:

www.mysite.com/halls.php?fairid=65 应该变成

www.mysite.com/fairname

而 www.mysite.com/companies.php?fairid=65&hallid=23 应该变成

www.mysite.com/fairname/hallname

你明白了。

我需要有关方法的帮助。在展览会、大厅和文章的表格中创建一个名为例如别名的字段然后尝试重写 url 是否是个好主意?任何人都可以帮助我完成如何创建这个脚本的步骤,或者给我指出更好的方法?

我擅长 php,但不擅长正则表达式,所以我会迷失在 .htaccess 部分。

任何帮助将不胜感激。

问候,佐兰

4

2 回答 2

5

.htaccess 类似:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) /url_rewrite.php?path=$1 [L,QSA]

数据库表类似于:

+------+--------+
| path | url    |
+------+--------+

以路径作为主键。

还有一个 PHP 脚本url_rewrite.php,它从 URL 中获取路径参数,在数据库中查找真正的 url 并执行 HTTP 301 重定向。

url_rewite.php 页面可能看起来像这样(这几乎是样板代码,需要根据您的要求进行调整 - 实际上我们不应该再使用 mysql_query() - PDO 或 MySQLi 更好但不是很好,已弃用)。

<?php
// -- database connection and any intialisation stuff you may have might go here ...

//get the path
$sPath = !empty($_GET['path']);

// -> error trapping for empty paths here

//retrieve the "real" url from the database
$dbQry = mysql_query("SELECT `url` FROM `seourls` WHERE `path` = '" . mysql_real_escape_string($sPath) . "' LIMIT 0, 1");

// -> error trapping here for query errors here

$dbResponse = mysql_fetch_row($dbQuery);

// -> error trapping here for empty recordsets here

$sRealPath = $dbResponse[0]; # which might be "/articles_en.php?artid=89"

//redirect
header("HTTP/1.1 302 Found");
header("Status: 302 Found"); # for Chrome/FastCGI implementation
header("Location: {$sRealPath}");
die();

?>
于 2012-05-23T14:20:17.877 回答
1

我建议您仔细查看 SO 上这个问题的漂亮 URL 并获得一些想法。不用说,SO 问题在 Google 搜索结果中的排名非常高。因此,从 SO URL 约定中获取线索,我建议您使用以下 3 种漂亮的 URL 格式:

  1. /articles/89/mytitle/articles_en.php?artid=89
  2. /halls/65/sometitle for/halls.php?fairid=65
  3. /companies/65-23/company/companies.php?fairid=65&hallid=23

现在创建 3个查找表articles,如下所示:hallscompanies

表:文章:

+-------+-------+
| 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:我使用normalizePHP 函数来获取所有小写的 URL 文本,清除特殊字符并将所有空格转换为连字符。

于 2012-05-23T21:16:50.343 回答