我有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部分.
任何帮助将深表感谢.
此致,佐兰
.htaccess之类的东西:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) /url_rewrite.php?path=$1 [L,QSA]
Run Code Online (Sandbox Code Playgroud)
数据库表类似于:
+------+--------+
| path | url |
+------+--------+
Run Code Online (Sandbox Code Playgroud)
将路径作为PRIMARY KEY.
并且调用的PHP脚本url_rewrite.php
从URL获取path参数,在数据库上查找以查找真实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();
?>
Run Code Online (Sandbox Code Playgroud)