Home  >  Article  >  PHP Framework  >  thinkphp5.0 sets pseudo-static

thinkphp5.0 sets pseudo-static

WBOY
WBOYOriginal
2023-05-29 13:21:083257browse

With the continuous development of Internet technology, the construction and maintenance of websites are becoming more and more convenient and simple. Now many people choose to use the PHP framework to build their own websites. Among them, ThinkPHP, as a PHP development framework, has become the first choice of many PHP developers. In ThinkPHP5.0 version, setting pseudo-static is a very basic but common task. This article will introduce readers to how to set pseudo-static in ThinkPHP5.0.

1. The concept of pseudo-static

Pseudo-static refers to converting URLs with parameters into a form that is more friendly to search engines and displaying them statically without changing the dynamic generation of the page. Static access to dynamic pages.

For example, a traditional dynamic URL link is: http://www.example.com/index.php?cat_id=1, disguise it as /static/1.html. At this time, when accessing http://www.example.com/static/1.html, the page effect is the same as accessing http://www.example.com/index.php?cat_id=1, but the former is more Be friendly.

ThinkPHP5.0 framework itself supports URL pseudo-static, which can better protect site security and also help improve natural search rankings.

2. Pseudo-static settings

In the ThinkPHP5.0 framework, implementing URL pseudo-static requires the following two steps:

  1. Modify the configuration file

Open the config directory in the project root directory and find the corresponding file:

app.php file: Modify the 'url_html_suffix' value and set it to the suffix we want, for example .html.

Routing configuration file route.php: Modify 'route_config_file' => 'route',

'url_route_on'              =>  true,    //开启路由
'url_route_must'            =>  true,     //所有路由必须在路由表中定义

where url_route_must is set to true. This setting is to ensure that all routes must be in the routing table If it is not defined, a 404 error will be thrown.

You also need to add routing rules in the route.php file:

//Configure routing rules
use thinkRoute;
Route::rule('detail-53384f78b45ee9f1e3082cf378b9c5b4 -8a11bc632ea32a57b3e3693c7987c420', 'index/article/detail');

In the above code snippet, we define a routing rule that will http://www.example.com/index/article /detail/id/1 is converted to http://www.example.com/detail-1.php. Among them, detail-53384f78b45ee9f1e3082cf378b9c5b4-8a11bc632ea32a57b3e3693c7987c420 is a variable routing rule format, and 53384f78b45ee9f1e3082cf378b9c5b4 and 8a11bc632ea32a57b3e3693c7987c420 are variable names respectively. Through this routing rule, we rewrite the dynamic URL of the article details page into a static URL.

  1. Configuring the Web server

If you want the set pseudo-static route to take effect, you also need to perform the corresponding configuration on the Web server. Taking the Apache server as an example, you need to add code to the Apache configuration file .htaccess:

Turn on RewriteEngine

RewriteEngine On

Define pseudo-static routing rules

RewriteRule ^detail-(d )-(.*)$ /index/article/detail/id/$1 [NC,L]

The above code implements if the URL link matches /detail-

3. Test the pseudo-static function

After completing the pseudo-static configuration, you need to test the set pseudo-static rules. Only after the test is OK can it be deployed to the online environment. When testing pseudo-static, you need to add the pseudo-static suffix to the URL, for example: http://www.example.com/detail-1.html. If the link can be accessed normally and points to a dynamic page, then it means The pseudo-static setup was successful.

4. Summary

ThinkPHP5.0 framework comes with pseudo-static function. With the help of the rewriting mechanism of the web server, combined with the built-in routing system of the framework itself and the function of generating URL, it can be realized Fast pseudo-static setup. However, in practical applications, many limitations such as SEO friendliness and URL specifications need to be taken into consideration, and pseudo-static solutions need to be flexibly used.

The above is the method of setting pseudo-static in thinkphp5.0 introduced in this article. With the development of technology, pseudo-static has become one of the basic configuration items for website construction. Understanding its charm will improve the SEO friendliness of the website. and user experience have been improved.

The above is the detailed content of thinkphp5.0 sets pseudo-static. 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