thinkphp5.0 sets pseudo-static
May 29, 2023 pm 01:21 PMWith 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:
- 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-
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-
- 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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)