PHP动态数据库页面重写URL [英] PHP dynamic DB page rewrite URL

查看:84
本文介绍了PHP动态数据库页面重写URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何制作www.mydomain.com/folder/?id=123 ---> www.mydomain.com/folder/xCkLbgGge

How can I make www.mydomain.com/folder/?id=123 ---> www.mydomain.com/folder/xCkLbgGge

我希望我的数据库查询页面获得它自己的URL,就像我在Twitter等上看到的一样.

I want my DB query page to get it's own URL, like I've seen on twitter etc etc.

推荐答案

这被称为"slug" wordpress,使该术语流行.无论如何.

This is known as a "slug" wordpress made this term popular. Anyway though.

最终您需要做的是拥有一个.htaccess文件,该文件可以捕获所有传入的流量,然后在服务器级别对其进行重新构造以在某种意义上与您的PHP一起使用,您仍将保持?id = 123逻辑不变,但是到客户端'/folder/FHJKD/'将是可见的结果.

Ultimately what you need to do is have an .htaccess file that catches all your incoming traffic then reforms it at the server level to work with your PHP in the sense, you will still keep the ?id=123 logic intact, but to the client side '/folder/FHJKD/' will be the viewable result.

这是一个.htaccess文件的示例,我在其上使用了类似的逻辑(.wordpress也是如此).

here is an example of an .htaccess file I use a similar logic on.. (so does wordpress for that matter).

RewriteEngine On
#strips the www out of the domain if there
RewriteCond %{HTTP_HOST} ^www\.domain\.com$

#applies logic that changes the domain from http://mydomain.com/post/my-article
#to resemble http://mydomain.com/?id=post/my-article
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?id=$1 [QSA,L]

要做的是将所有内容都放在domain.com/之后,并将其作为变量传递给index.php,在此示例中,该变量将是"id",因此您必须提供最适合您站点需求的逻辑.

what this will do is take everything after domain.com/ and pass it as a variable to index.php the variable in this example would be 'id' from this you have to device a logic that best suits your sites needs.

示例

<?php
 //the URL for the example here: http://mydomain.com/?id=post/my-article
 if($_GET['id'])
 {
   $myParams = explode('/', $_GET['id']);
   echo '<pre>';
   print_r($myParams);
   echo '</pre>';
 }
?>

现在,对此的逻辑必须更深入,这只是基本的纯示例,但总体而言,尤其是导致您使用数据库的工作,我想确保$ myParams不受恶意软件的侵害.代码,可以注入到您的PHP或数据库中.

now the logic for this would have to go much deeper, this is only pure example at a basic level, but overall and especially cause your working with a database I assume, your gonna wanna make sure the $myParams is clean of malicious code, that can inject into your PHP or Database.

通过print_r()的上述$myParams的输出为:

Array(
   [0] => post
   [1] => my-article
)

要使用它,至少需要做

echo $myParams[0].'<br />';

或者您可以这样做,因为大多数浏览器都会添加最终的/

or you could do it like this cause most browsers will add a final /

<?php
 //the URL for the example here: http://mydomain.com/?id=post/my-article
 if($_GET['id'])
 {
   //breaks the variable apart, removes any empty array values and reorders the index
   $myParams = array_values(array_filter(explode('/', $_GET['id'])));
   if(count($myParams > 1)
   {
       $sql = "SELECT * FROM post_table WHERE slug = '".mysql_real_escape_string($myParams[1])."'";
       $result = mysql_query($sql);
   }

 }
?>

现在可以肯定的是,这是一个非常粗糙的示例,您将需要在其中工作以防止mysql注入逻辑,然后将应用查询,就像现在使用id = 123提取文章一样

Now this admitedly is a very crude example, you would want to work some logic in there to prevent mysql injection, and then you will apply the query like you would how you are now in pulling your articles out using just id=123.

或者,您也可以走一条完全不同的路线,并探索MVC(模型视图控件)的奇迹.像CodeIgniter这样的东西是一个很好的简单MVC框架,可以开始使用.但这取决于你.

Alternatively you could also go a completely different route, and explore the wonders of MVC (Model View Control). Something like CodeIgniter is a nice easy MVC framework to get started on. But thats up to you.

这篇关于PHP动态数据库页面重写URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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