The pseudo static implementation based on php is described in detail

  • 2020-06-01 08:37:44
  • OfStack

1. Operate the implementation according to $_SERVER['PATH_INFO'].
For example, the address of your website is http://127.0.0.1/ show_new.php/look-id-1.shtml
You echo $_SERVER['PATH_INFO'] will come out with/look-id-1.shtml saw this and I think you get the idea.
The complete demo
index.php


index.php 
$conn=mysql_connect("localhost","root","root")or dir(" The connection fails ");
mysql_select_db("tb_demo",$conn);
$sql="select * from news";
$res=mysql_query($sql);
header("content-type:text/html;charset=utf-8");
echo "<h1> News list </h1>";
echo "<a href='add_news.html'> Add the news </a><hr/>";
echo "<table>";
echo "<tr><td>id</td><td> The title </td><td> Check the details </td><td> Modify the news </td></tr>";
while($row=mysql_fetch_assoc($res)){
 echo "<tr><td>{$row['id']}</td><td>{$row['title']}</td><td><a href='show_new.php/look-id-{$row['id']}.shtml'> Check the details </a></td><td><a href='#'> Modify the page </a></td></tr>";
}
// The red address on the top should have been show_news.php?act=look&id={$row['id']}
echo "</table>";
// Close the resource 
mysql_free_result($res);
mysql_close($conn);

show_new php page

show_new.php 
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'];
//$pa   The printed value is   /look-id-1.html
// Obtained through regular expression matching url address 
if(preg_match('/^\/(look)-(id)-([\d])\.shtml$/',$pa,$arr)){
 $act = $arr[1]; // This is requested look methods 
 $id = $arr[3];  // This is what we get 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 Illegal address ";
}
mysql_close($conn);

I think you get the idea when you look at this one up here. In fact, we don't use it that much. Let me give you the second one

2. According to the configuration.htaccess.
So how do you create an htaccess file, create a notepad in the root directory of the website and then double click open and click save as file name as written
.htaccess, save type select all files, code select utf-8 code okay so that's where you see this.htaccess file in the directory

First turn on mod_rewrite.so,AllowOverride None, AllowOverride All in two places

For example, href address is one_new-id-1.shtml // this means one_new.php? id = 1
So this is htaccess right here


<IfModule rewrite_module>
# Write your rewrite The rules 
RewriteEngine On
#  Multiple rules can be configured, matching from top to bottom 
RewriteRule  one_new-id-(\d+)\.shtml$ one_new.php?id=$1 // Here, $1  It stands for no 1 A parameter? 
RewriteRule  abc_id(\d+)\.html$     error.php
# Set up the 404 error 
#ErrorDocument  404  /error.php
</IfModule>

echo $_GET['id'] will definitely output the value of id on one_new.php

Note: the current personal ability can only write here I will gradually improve
Leave me a message if you have any questions


Related articles: