Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
839 views
in Technique[技术] by (71.8m points)

url rewriting - PHP dynamic DB page rewrite URL

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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]

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.

example

<?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>';
 }
?>

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.

The output of the above $myParams via print_r() would be:

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

To work with it you would need to do at the very least

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);
   }

 }
?>

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...