Home > Article > Backend Development > Detailed introduction to the implementation of pseudo-static based on PHP_PHP tutorial
1. Operate based on $_SERVER['PATH_INFO'].
For example, the address of your website is http://127.0.0.1/show_new.php/look-id-1.shtml
The result of your echo $_SERVER['PATH_INFO'] It will be /look-id-1.shtml. After seeing this, I think everyone may have understood it.
Complete demo
index.php
$conn=mysql_connect("localhost","root","root")or dir("Connection failed");
mysql_select_db("tb_demo",$conn);
$sql="select * from news";
$res=mysql_query($sql);
header("content-type:text/html;charset=utf-8");
echo "
id | title | View details | Modify news |
{$row['id']} | < ;td>{$row['title']}View Details | Modify page |
header("Content-type:text/html;charset=utf-8");
$conn=mysql_connect("localhost","root","root");
mysql_select_db("tb_demo ",$conn);
mysql_query("set names utf8");
$pa = $_SERVER['PATH_INFO'];
//The printed value of $pa is /look-id-1 .html
//URL address obtained through regular expression matching
if(preg_match('/^/(look)-(id)-([d]).shtml$/',$pa,$ arr)){
$act = $arr[1]; //This is the requested look method
$id = $arr[3]; //This is the obtained id value
$sql= "select * from news where id= $id";
$res=mysql_query($sql);
$res = mysql_fetch_assoc($res);
echo $res['title']."< ;hr>".$res['content'];
}else{
echo "url address is illegal";
}
mysql_close($conn);
2. Implement according to configuration .htaccess.
Let’s first talk about how to create the .htaccess file. Create a notepad in the root directory of the website and then double-click to open it. Click Save as and write the file name as
.htaccess. Select all files as the save type and utf- as the encoding. After 8 is encoded, you will see this .htaccess file in the directory
First, open mod_rewrite.so in apache, AllowOverride None. There are two places here that are replaced with AllowOverride All
For example, href The address is written as one_new-id-1.shtml //This means one_new.php?id=1
The .htaccess here can be written like this
Note: Currently my personal ability can only be written here. I will gradually improve it in the future
If you have any questions, please leave me a message