Home >Backend Development >PHP Problem >How to do pseudo-static processing in php

How to do pseudo-static processing in php

(*-*)浩
(*-*)浩Original
2019-09-19 10:30:164901browse

What is pseudo-static:

How to do pseudo-static processing in php

Websites are divided into dynamic websites and static websites. Our common ones ending with html are generally static pages. , pages ending with .php.asp are generally dynamic websites. Dynamic websites are called dynamic websites when they interact with the database. Dynamic websites are not easily indexed by search engines, so they are called pseudo-static. (Recommended learning: PHP programming from entry to proficiency)

Pseudo-static, as the name suggests, is a fake static website. In other words, in order to facilitate the inclusion of search engines, dynamic websites are implemented using the server's rewirte False static process.

So how many ways are there to implement pseudo-static in PHP?

1, Use apache’s URL rewriting rules, everyone knows this, configure it in apache, everyone here has made it, just list a simple configuration

RewriteEngine On
RewriteRule ^/test.html index.php?controller=index&action=test [L]

2, Use PHP's pathinfo , Have you seen some websites doing this 'www.xxx.com/index.php/c/index/a/test/id/100 ', of course to support this you need to set the parameter

'cgi.fix_pathinfo' in 'php.ini' to 1. Take 'www.xxx.com/index.php/c/index/a/test/id/100' as an example

echo $_SERVER['PATH_INFO']; //输出'/c/index/a/test/id/100'

You should understand it now, you can parse this paragraph and assign it The actual address

3, uses the 404 mechanism. Generally, pseudo-static pages are pages that do not actually exist, so you can use apache 404 configuration, but there are some problems, which are of the 'post' type. The request will be abandoned, causing you to be unable to obtain '$_POST',

But '$_GET' can still be obtained. Assume that the 404 page here is '404page.php', and the apache configuration is as follows:

ErrorDocument 404 /404page.php

Then embed the following code in '404page.php'

header("HTTP/1.1 200 OK"); //这里一定要有,不然状态就是404

$reqUrl = $_SERVER['REQUEST_URI']; // 请求地址

/**
* 从URL中解析参数
*/
function parseUrlParams($queryUrl)
{
    $arr = explode('?', $queryUrl);
    parse_str($arr[1], $param);
    if($param)
    {
        foreach($param as $key => $value)
        {
            $_GET[$key] = $value;
        }
    }
}

parseUrlParams($reqUrl); // url解析参数

//然后你就可以使用 $reqUrl 根据自己的规则匹配不同的实际请求地址
if(preg_match('#^/test.html#is', $reqUrl, $matches))
{
   include('index.php');
   die();
}

The above is the detailed content of How to do pseudo-static processing in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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