我有php应用程序必须部分重写,因为客户要求拥有SEO frendly urls。
我的链接如下:
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
你明白了。
我需要帮助这个方法。在展览会,大厅和文章的表格中,在表格中创建一个以别名命名的字段然后尝试重写网址是不是一个好主意?任何人都可以帮助我完成如何创建此脚本的步骤,或指向我更好的方法?
我擅长php,但对正则表达式并不擅长,所以我会迷失在.htaccess部分。
任何帮助都将深受赞赏。
关心,佐兰
答案 0 :(得分:5)
.htaccess之类的东西:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) /url_rewrite.php?path=$1 [L,QSA]
数据库表类似于:
+------+--------+
| path | url |
+------+--------+
将路径作为PRIMARY KEY。
一个名为url_rewrite.php
的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();
?>
答案 1 :(得分:1)
我建议你在SO上仔细查看这个问题的漂亮网址并获得一些想法。不用说,在Google搜索结果中,SO问题排名很高。因此,从SO URL约定中获取线索,我建议您使用以下3种漂亮的URL格式:
/articles_en.php?artid=89
{li> / halls / 65 / sometitle /halls.php?fairid=65
/companies.php?fairid=65&hallid=23
现在创建3个查找表articles
,halls
和companies
,如下所示:
表:文章:
+-------+-------+
| artid | title |
+-------+-------+
大厅:
+--------+-------+
| fairid | title |
+--------+-------+
表公司:
+--------+--------+------+
| fairid | hallid | name |
+--------+--------+------+
现在,对于上述3个漂亮的网址处理,请在$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]
最后让您的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
行以获得更好的搜索引擎优化结果。
PS:我使用normalize
PHP函数以小写形式获取所有网址文字,清理特殊字符并将所有空格转换为连字符。