我有 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 部分。
任何帮助将不胜感激。
问候, 佐兰
.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();
?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)