Home > Backend Development > PHP Tutorial > Analyze the implementation method based on PHP pseudo-static

Analyze the implementation method based on PHP pseudo-static

coldplay.xixi
Release: 2023-04-09 12:34:01
forward
2605 people have browsed it

Analyze the implementation method based on PHP pseudo-static

I have been doing PHP development work. During the development process, I have heard about "pseudo-static" for a long time. But it has never been understood.

I finally made the decision today to have a good understanding of this aspect.

First of all, what is pseudo-static:

Pseudo-static, also known as URL rewriting, is a dynamic URL that looks like a static URL. In other words, dynamic web pages remove the parameters of dynamic web pages by rewriting the URL method, but there is no need to implement rewritten pages in the actual web page directory.

In addition, two noun parsing are being added

Static URL: Pure static HTML document, web page that can be queried using filetype:htm

Dynamic URL: The content is stored in the database , display the content according to the requirements, the URL starts with? # & Display different parameters, such as: news.php? lang=cn&class=1&id=2

Related learning recommendations: PHP programming from entry to proficiency

##Dynamic, static , The pros and cons between pseudo-static (new)

Dynamic URL

First of all, dynamic URLs are currently "unable" for Google. The statement "crawled" is wrong. Google can handle dynamic URLs very well and crawl them smoothly. Secondly, the statement "parameters cannot exceed 3" is also incorrect. Google can crawl dynamic URLs with more than 3 parameters. However, , parameters should be minimized to avoid the URL being too long.

Secondly, dynamic URLs have certain advantages. As mentioned above, news.php? lang=cn&class=1&id=2 For example, the parameters in the URL accurately tell Google that the content language of this URL is cn, it belongs to category 1, and the content ID is 2, which makes it easier for Google to identify and process the content.

Finally, dynamic URLs should be as concise as possible, especially the session identifier (sid) and query (query) parameters, which can easily lead to a large number of identical pages.

Static URL

First of all, the absolute advantage of static URL is its clarity. /product/nokia/n95.html and /about.html can be easily understood. , which may result in a relatively high number of clicks in the search results.

Secondly, static URLs may not be the best URL form. As mentioned above, dynamic URLs can tell Google some identifiable parameters, while static URLs will not be properly arranged if the document layout is not appropriate (such as too flat). , placing all HTML documents in the root directory) and other factors, it is not as rich in reference information as static URLs provide to Google.

Finally, does Le Sishu think there is any hidden meaning in Google’s article? "Updating pages with this type of URL can be time-consuming, especially when the amount of information grows quickly, because the compilation code must be changed for each individual page." Although it is a website, is it the same in the Google system? Does such a problem exist?

Pseudo-static URL

First of all, pseudo-static URLs cannot make dynamic URLs "static". Pseudo-static URLs are just a rewrite of dynamic URLs. Google will not Think of pseudo-static as an HTML document.

Secondly, pseudo-static is desirable, but the focus should be on removing redundant parameters, standardizing URLs, and avoiding duplicate pages as much as possible.

Finally, pseudo-static has great potential dangers. It is best to use it when you are familiar with the website system, website structure, content distribution, and parameter meaning.

When writing pseudo-static rules, you should retain valuable parameters and do not streamline all valuable parameters, such as news.php in the previous example? lang=cn&class=1&id=2 is better rewritten as news-cn-class1-id2.html rather than overly concise and rewritten as news-2.html.

Furthermore, the pseudo-static must not contain session ID (sid) and query (query) parameters, /product.asp? For a dynamic URL like sid=98971298178906&id=1234, the sid in it is originally recognized and blocked by Google. However, if it is rewritten as /product/98971298178906/1234, Google will not only be unable to recognize it, but will also cause infinite duplicate pages in the entire site (each session A new session ID will be generated).

Should we choose pseudo-static or true static

1. There is no difference between using true static and false static for SEO

2. Using true static may cause hard disk damage Damage will affect forum performance

 3. Using pseudo-static will occupy a certain amount of CPU occupancy, and heavy use will cause CPU overload

 4. The most important point is that we need static For SEO

So:

1. Using the true static method can be directly eliminated, because no matter how it is generated, it will be very harmful to the hard disk.

 2. Since the effects of true and false static are the same, we can choose pseudo-static.

 3. However, extensive use of pseudo-static will cause CPU overload.

 4. So as long as we don’t use it in large quantities, it’s fine.

5. Since static is only for SEO, we only need pseudo-static for SEO, and there is no need for users to use it.

 6. So we only need to use pseudo-static in the Archiver specially provided for SEO crawling.

 7. Thank you all for your patience in reading my article.

 8. If you have any questions or have different opinions, please feel free to make

comments about pseudo-static and true static

There is an essential difference between true static and pseudo-static of. Processing a pure HTML for browsing users and a PHP that calls multiple data have significantly less CPU usage than the former. I remember someone once said that the hard disk is frequently read and written when downloading HTML. He said this as if reading the database does not require reading and writing to the disk. What's more, there are a lot of cached scattered PHP that are also placed on the hard disk. Doesn't these reads require disk operations? ridiculous.

The purpose can be achieved by reading a single html, picture, Flash and other attachments. Why bother reading the database, reading the PHP cache file, re-integrating the data output, and then reading the picture, Flash and other attachments? The CMS home page does not require a lot of interaction, and the forum version should not be used here. On the contrary, what should be considered more is: beauty! compatible! Intuitive information! performance! And stability!

Four ways to implement pseudo-static transfer in PHP:

 <?php
 //伪静态方法一
 
 // localhost/php100/test.php?id|1@action|2
 $Php2Html_FileUrl = $_SERVER["REQUEST_URI"];
 echo $Php2Html_FileUrl."<br>";// /php100/test.php?id|1@action|2
 $Php2Html_UrlString = str_replace("?","",str_replace("/", "", strrchr(strrchr($Php2Html_FileUrl, "/"),"?")));
 echo $Php2Html_UrlString."<br>";// id|1@action|2
 $Php2Html_UrlQueryStrList = explode("@", $Php2Html_UrlString);
 print_r($Php2Html_UrlQueryStrList);// Array ( [0] => id|1 [1] => action|2 )
 echo "<br>";
 foreach($Php2Html_UrlQueryStrList as $Php2Html_UrlQueryStr)
 {
 $Php2Html_TmpArray = explode("|", $Php2Html_UrlQueryStr);
 print_r($Php2Html_TmpArray);// Array ( [0] => id [1] => 1 ) ; Array ( [0] => action [1] => 2 )
 echo "<br>";
 $_GET[$Php2Html_TmpArray[0]] = $Php2Html_TmpArray[1];
 }
 //echo &#39;假静态:$_GET变量<br />&#39;;
 print_r($_GET); // Array ( [id|1@action|2] => [id] => 1 [action] => 2 )
 echo "<br>";
 echo "<hr>";
 echo $_GET[id]."<br>";// 1
 echo $_GET[action];// 2
 ?>
Copy after login
 <?php
 //伪静态方法二
 
 // localhost/php100/test.php/1/2
 $filename = basename($_SERVER[&#39;SCRIPT_NAME&#39;]);
 echo $_SERVER[&#39;SCRIPT_NAME&#39;]."<br>";// /php100/test.php
 echo $filename."<br>";// test.php
 
 if(strtolower($filename)==&#39;test.php&#39;){
 if(!empty($_GET[id])){
 $id=intval($_GET[id]);
 echo $id."<br>";
 $action=intval($_GET[action]);
 echo $action."<br>";
 }else{
 $nav=$_SERVER[&#39;REQUEST_URI&#39;];
 echo "1:".$nav."<br>";// /php100/test.php/1/2
 $script=$_SERVER[&#39;SCRIPT_NAME&#39;];
 echo "2:".$script."<br>";// /php100/test.php
 $nav=ereg_replace("^$script","",urldecode($nav));
 echo $nav."<br>"; // /1/2
 $vars=explode("/",$nav);
 print_r($vars);// Array ( [0] => [1] => 1 [2] => 2 )
 echo "<br>";
 $id=intval($vars[1]);
 $action=intval($vars[2]);
 }
 echo $id.&#39;&&#39;.$action;
 }
 ?>
Copy after login
<?php
 //伪静态方法三 
 function mod_rewrite(){
 global $_GET;
 $nav=$_SERVER["REQUEST_URI"];
 echo $nav."<br>";
 $script_name=$_SERVER["SCRIPT_NAME"];
echo $script_name."<br>";
$nav=substr(ereg_replace("^$script_name","",urldecode($nav)),1);
echo $nav."<br>";
$nav=preg_replace("/^.ht(m){1}(l){0,1}$/","",$nav);//这句是去掉尾部的.html或.htm
echo $nav."<br>";
$vars = explode("/",$nav);
print_r($vars);
echo "<br>";
for($i=0;$i<Count($vars);$i+=2){
$_GET["$vars[$i]"]=$vars[$i+1];
}
return $_GET;
 }
 mod_rewrite();
 $year=$_GET["year"];//结果为&#39;2006&#39;
 echo $year."<br>";
$action=$_GET["action"];//结果为&#39;_add&#39;
 echo $action;
 ?>
Copy after login
 <?php
 //伪静态方法四
 
 //利用server变量 取得PATH_INFO信息 该例中为 /1,100,8630.html 也就是执行脚本名后面的部分
 if(@$path_info =$_SERVER["PATH_INFO"]){
 //正则匹配一下参数
 if(preg_match("/\/(\d+),(\d+),(\d+)\.html/si",$path_info,$arr_path)){
 $gid  =intval($arr_path[1]); //取得值 1
 $sid  =intval($arr_path[2]); //取得值100
$softid =intval($arr_path[3]); //取得值8630
}else die("Path:Error!");
//相当于soft.php?gid=1&sid=100&softid=8630
}else die(&#39;Path:Nothing!&#39;);
?>
Copy after login

If you don’t want to use PHP to implement pseudo-static, you can use the url rewrite that comes with servers such as apache, nginx, iis etc. function to set.

The above is the detailed content of Analyze the implementation method based on PHP pseudo-static. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template