重写PHP应用程序来获得搜索引擎友好的URL(rewriting php application t

2019-07-29 12:46发布

我有再因为客户要求有SEO员工共赢的URL被部分重写PHP应用程序。

我的链接如下:

www.mysite.com/articles_en.php?artid=89,在那里我将不得不更改URL在此:

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的一部分。

任何帮助将十分赞赏。

问候,卓然

Answer 1:

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();

?>


Answer 2:

我建议你仔细看的SO这个问题的漂亮的网址,并得到一些想法。 不必说做题的排名在谷歌搜索结果中非常高。 因此,采取从SO URL约定的线索,我建议你这3种漂亮的URL格式:

  1. /文章/ 89 / mytitle/articles_en.php?artid=89
  2. /室/ 65 / sometitle /halls.php?fairid=65
  3. /公司/ 65-23 /公司 /companies.php?fairid=65&hallid=23

现在,创建3个查找表articleshallscompanies是这样的:

表:文章:

+-------+-------+
| 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文本,清理特殊字符,然后转换所有空格连字符。



文章来源: rewriting php application to get seo friendly url's