简体   繁体   English

重写php应用程序以获得seo友好的URL

[英]rewriting php application to get seo friendly url's

I have php application which have to be partially rewritten because of the customer request to have SEO frendly urls. 我有php应用程序必须部分重写,因为客户要求有SEO frendly urls。

My links are as follow: 我的链接如下:

www.mysite.com/articles_en.php?artid=89, where I will have to change the url in this: www.mysite.com/articles_en.php?artid=89,我必须在这里更改网址:

www.mysite.com/articleTitle www.mysite.com/articleTitle

Then I have this url: 然后我有这个网址:

www.mysite.com/halls.php?fairid=65 which should become 应该成为www.mysite.com/halls.php?fairid=65

www.mysite.com/fairname www.mysite.com/fairname

And www.mysite.com/companies.php?fairid=65&hallid=23 which should become 并且www.mysite.com/companies.php?fairid=65&hallid=23应该成为

www.mysite.com/fairname/hallname www.mysite.com/fairname/hallname

You get the idea. 你明白了。

I need a help with the approach. 我需要帮助这个方法。 Is it good idea in the tables of the fairs, halls and articles to create a field in the table named for example alias and then to attempt to rewrite the url? 在展览会,大厅和文章的表格中,在表格中创建一个以别名命名的字段然后尝试重写网址是不是一个好主意? Anyone can help me with the steps how to create this script, or to point me to better approach? 任何人都可以帮助我完成如何创建此脚本的步骤,或指向我更好的方法?

I am good at php, but not good at all with regular expressions, so I will be lost on the .htaccess part. 我擅长php,但对正则表达式并不擅长,所以我会迷失在.htaccess部分。

Any help will be deeply appreciated. 任何帮助将深表感谢。

Regards, Zoran 此致,佐兰

.htaccess something like: .htaccess之类的东西:

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

database table something like: 数据库表类似于:

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

With the path as the PRIMARY KEY. 将路径作为PRIMARY KEY。

And a PHP script called url_rewrite.php that takes the path parameter from the URL, does a lookup on the database to find the real url and performs an HTTP 301 redirect. 并且一个名为url_rewrite.php的PHP脚本从URL获取path参数,在数据库上查找以查找真实URL并执行HTTP 301重定向。

The url_rewite.php page might look something like this (this is pretty much boilerplate code and will need adjusting to fit your requirements - realistically we shouldn't be using mysql_query() any more either - PDO or MySQLi are better and aren't well, deprecated). 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();

?>

I suggest you to carefully look at the pretty URL of this question on SO and get some idea. 我建议你仔细看看这个问题的漂亮网址,并得到一些想法。 Needless to add that SO questions rank very high on Google search results. 不用说,在Google搜索结果中,SO问题排名很高。 So taking a clue from SO URL conventions, I suggest you these 3 pretty URL formats: 因此,从SO URL约定中获取线索,我建议您使用以下3种漂亮的URL格式:

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

Now create 3 lookup tables articles , halls and companies like this: 现在创建3个查找表articleshallscompanies如下所示:

Table: articles: 表:文章:

+-------+-------+
| artid | title |
+-------+-------+

Table halls: 桌厅:

+--------+-------+
| fairid | title |
+--------+-------+

Table companies: 表公司:

+--------+--------+------+
| fairid | hallid | name |
+--------+--------+------+

Now for above 3 pretty URL handling add this code in your .htaccess under $DOCUMENT_ROOT : 现在,对于上面3个漂亮的URL处理,在$DOCUMENT_ROOT下的.htaccess中添加此代码:

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]

Finally have your router.php code like this: (sample code) 最后你的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));
}
?>

Once you verify that it's working fine uncomment 301 Moved Permanently lines to get better SEO results. 一旦你验证它工作正常取消注释301 Moved Permanently线以获得更好的SEO结果。

PS: I have used normalize PHP function to get all URL text in lowercase, cleaning up special characters and converting all spaces to hyphens. PS:我使用了normalize PHP函数来获取小写的所有URL文本,清理特殊字符并将所有空格转换为连字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM