重写PHP应用程序来获得搜索引擎友好的URL [英] rewriting php application to get seo friendly url's

查看:198
本文介绍了重写PHP应用程序来获得搜索引擎友好的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的PHP应用程序,它有因为客户要求有搜索引擎优化员工共赢url以进行部分改写。

I have php application which have to be partially rewritten because of the customer request to have SEO frendly urls.

我的链接如下:

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

www.mysite.com/articles_en.php?artid=89, where I will have to change the url in this:

www.mysite.com/articleTitle

www.mysite.com/articleTitle

然后,我有这个网址:

www.mysite.com/halls.php?fairid=65这应该成为

www.mysite.com/halls.php?fairid=65 which should become

www.mysite.com/fairname

www.mysite.com/fairname

和www.mysite.com/companies.php?fairid=65&hallid=23这应该成为

And www.mysite.com/companies.php?fairid=65&hallid=23 which should become

www.mysite.com/fairname/hallname

www.mysite.com/fairname/hallname

您的想法。

我需要帮助的办法。难道是交易会,大厅和文章,名为例如别名表中创建一个字段,然后在表中试图重写URL好主意吗?任何人都可以帮助我的步骤,如何创建该脚本,或者给我点到更好的办法?

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?

我擅长PHP,但也不好定期EX pressions,所以我将在htaccess的部分丢失。

I am good at php, but not good at all with regular expressions, so I will be lost on the .htaccess part.

任何帮助会深深AP preciated。

Any help will be deeply appreciated.

问候,卓然

推荐答案

的.htaccess是这样的:

.htaccess something like:

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

数据库中的表是这样的:

database table something like:

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

通过的路径作为主键。

和调用的PHP脚本 url_rewrite.php ,是以path参数的URL,做了查找数据库来找到真正的URL和执行HTTP 301重定向

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_rewite.php页可能是这个样子(这是pretty的很多样板code和需要调整,以适应您的需求 - 现实,我们不应该使用的mysql_query()更多或者 - PDO或MySQLi的更好,不是很好,去precated)。

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).

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

?>

这篇关于重写PHP应用程序来获得搜索引擎友好的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆