Table of Contents
There are four ways to achieve pseudo-static in PHP, do you think of them? , No. 4 Middle School
Home Backend Development PHP Tutorial There are four methods to achieve pseudo-static in PHP, do you know them? , No. 4 Middle School_PHP Tutorial

There are four methods to achieve pseudo-static in PHP, do you know them? , No. 4 Middle School_PHP Tutorial

Jul 12, 2016 am 09:01 AM

There are four ways to achieve pseudo-static in PHP, do you think of them? , No. 4 Middle School

Speaking of the pseudo-static implementation plan, are you very happy to answer "It's simple, just configure the rewrite rules of apache and that's it"

But have you noticed this situation? You have recently developed a lot of new features. Several new features are added every day, and there are many pseudo-static configurations every day. It has only been two days since the operation and maintenance, and the classmates are willing to cooperate. After two days, the operation and maintenance Classmate Wei was about to scold him. Why don't you do it all at once and bother me every day because you are so numb and stupid? But, you are about to go online, and you have to ask the operation and maintenance classmates hard, and then say the most shameless words in the programmer world: "This is the last change", and then you have to change it again. , Sigh, your personality is completely ruined. . .

If you have such troubles, please read the following article to ensure that you will never ask for operation and maintenance again in the future, and you can do whatever you want. . .

So how many ways are there to implement pseudo-static in PHP? Personal opinions and statistics, there are four methods


1. Use apache’s URL rewriting rules. Everyone knows this. Configure it in apache. Here, students all made it. I only list a simple configuration

<span>RewriteEngine On
RewriteRule </span>^/test.html index.php?controller=index&action=test [L]
Copy after login

2. Use PHP's pathinfo. Have you seen some websites playing 'www.xxx.com/index.php/c/index/a/test/id/100' like this? Of course, this must be supported. You need to put the parameters in 'php.ini'

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

<span>echo</span> <span>$_SERVER</span>['PATH_INFO']; <span>//</span><span>输出'/c/index/a/test/id/100'</span>
Copy after login

At this point, you should understand it. You can then parse this paragraph and assign the actual address

3. Use the 404 mechanism. Generally, pseudo-static pages are pages that do not actually exist, so you can use apache 404 configuration. However, there are some problems. 'post' type requests 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
Copy after login

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

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

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

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

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

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

4. An improved version of method 3. Method 3 is equivalent to redirection in the internal mechanism of apache, causing the parameters passed by post(get) to be unobtainable. Analysis of the above actually shows that the relevant files cannot be found. When the server cannot find the relevant files, we specify a file for it. If it is OK, it will not need to jump. At this time, POST and the like will not be lost. The apache configuration is as follows:

<span>RewriteEngine On
RewriteCond </span>%{REQUEST_FILENAME} !-<span>f
RewriteCond </span>%{REQUEST_FILENAME} !-<span>d
RewriteRule </span>. index.php
Copy after login

The general meaning of the above configuration is that when the requested file or directory cannot be found, use 'index.php' in the root directory instead. Then you can get the relevant parameters in 'index.php' and parse to Actual request address

<span>/*</span><span>*
* 获取当前请求的URI地址
*@param void
*@author painsOnline
*@return string URI
</span><span>*/</span>
<span>function</span><span> getReqUri()
{
    </span><span>return</span> <span>trim</span>(<span>$_SERVER</span>["REQUEST_URI"<span>]);
}

</span><span>$reqUri</span> =<span> getReqUri();

</span><span>if</span>(<span>preg_match</span>('/^\/test.html/isU', <span>$reqUri</span><span>))
{</span><span>//</span><span>解析请求地址</span>
    <span>include</span> 'test.php'<span>;
    </span><span>exit</span><span>();
}</span>
Copy after login

I have little talent and knowledge, and I have some shortcomings. Welcome to make up for them

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1090919.htmlTechArticleThere are four ways to achieve pseudo-static in PHP, do you think of it? , No. 4 Middle School When talking about the pseudo-static implementation plan, do you readily answer "It's simple, just configure the rewrite rules of apache...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles