沉冰浮水

沉冰浮水

做最终到的事,成为最终成为的人!
github
bilibili
mastodon
zhihu
douban

"Water hole" a slightly in-depth explanation of knowledge related to pseudo-static

In theory, the URL points to a file, and even if it is a directory, it will look for index.html or index.php according to the default settings. If it does not exist, the web environment will return a 404 error.

However, for dynamic sites like Z-BlogPHP, the URL points to index.php, and then the internal logic determines whether to throw a 404 error.

The implementation of pointing the URL to index.php is called "URL Rewrite," which is commonly referred to as "pseudo-static" in Chinese, although strictly speaking, "pseudo-static" is only one application of "URL Rewrite."

Redirect is a "redirect (301)," which can be understood as a Rewrite with a 301 flag (?).

Why is it called "Water Pit":

[Index of "Water Pit" Tutorials](/post/20200617652.html "Index of "Water Pit" Tutorials")

The code and explanatory comments can be found in the Git link below.

Each link corresponds to a "Git commit" status, which is a "version control." You can copy and save them in order to test them in your web environment, such as URL_Rewrite/index.php.

Once you have a general understanding, you can replace it with the next "version" in the same file to see the effect.


Pure dynamic mode - php-rewrite/index.php · 70063e1 · wdssmq/StaleCode - Gitee.com

https://gitee.com/wdssmq/StaleCode/blob/70063e12f67c31ba5588a2b2a0a0e3fe727ea37b/php-rewrite/index.php


php-rewrite · wdssmq/StaleCode - Gitee - Open Source China:

https://gitee.com/wdssmq/StaleCode/tree/c04e9f96f4654f484510b93d7453ebf0f6c8b53f/php-rewrite

↑ You can view the screenshot of the effect here (pure dynamic mode)

You will find that the "not found" returned by /id=1024 and /post/2048.html are different. The 404 error of the latter is returned by the "web program."


"- It's already 00:41:31, so I'll submit it and continue tomorrow. -"

"- Added a few more sentences, so it's now 00:46:31. -"


So, let's continue. In the previous step, $options["is_rewrite"] was already defined as the switch for pseudo-static.

Now, change the value to true and add some code.


Targeted pseudo-static - php-rewrite/index.php · 6464486 · wdssmq/StaleCode - Gitee.com:

https://gitee.com/wdssmq/StaleCode/blob/6464486539fdb4f53a9b834d2a2d444587f14fe8/php-rewrite/index.php


The above URL is the complete code of the index.php file in this version. You can view the "file changes" information of this commit through the link on the right Targeted pseudo-static - php-rewrite/index.php · 6464486 · View file changes

The added PHP code is as follows. The basic concept is to "open the pseudo-static option" within the website program and then "change the content (mainly links) output to the webpage" based on the option.

// Pseudo-static switch toggle
$options["is_rewrite"] = true;

// Change the article URL to a static form
if ($options["is_rewrite"]) {
  $post["url"] = $options["host"] . "post/3.html";
}

↑ It is recommended to use the "file changes" link above to see that besides modifying the content of index.php, there is also an additional .htaccess file.

Only by correctly configuring .htaccess can the desired pseudo-static effect be fully achieved.

Note: .htaccess is the configuration file for Apache. IIS and Nginx also have their own rule syntax.

Summary:

PHP code and files belong to the "website program."

IIS/Nginx/Apache can be referred to as "web programs," which, together with PHP or other language parsing engines, and MySQL or other databases, form the "web environment."

The "URL Rewrite" mentioned at the beginning is a feature provided by the "web program."

In the usual sense, for Z-BlogPHP or similar programs, enabling pseudo-static requires:

  • Support from the program itself.
  • Enable the corresponding option switch.
  • Correctly configure the rule file corresponding to the "web program."

Other things to know:

  • After enabling pseudo-static, both /?id=3 and /post/3.html can be accessed, and the content accessed is the same. It is not a bug that the former cannot be "redirected (301)" to the latter in this example.
  • After enabling pseudo-static, both ?id=1024 and post/2048.html return a "not found" message from PHP.
  • When PHP outputs a 404 error, the status code should also be set:
    if ($status == "404") {
      header("HTTP/1.1 404 Not Found");
      header("status: 404 Not Found");
    }
    
  • The "redirect (301)" function can be implemented by both the "website program" and the "web program." For regular cases, it is strongly recommended to be implemented by the "web program."
    • Well... it seems that both 301 and 302 are considered "redirects," just with the difference between permanent and temporary.
    • 302 is generally used by the "website program" itself.
  • In this article, only the pseudo-static rules for "articles" are configured. In actual programs, there may also be "tags," "categories," "dates," and so on. Writing separate Rewrite rules for each of them seems to be more cumbersome.

"- That's about it. It's already noon after finishing writing. -"

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.