ThinkPHP作為PHP框架,是單一入口的,那么其原始的URL便不是那么友好,但ThinkPHP提供了各種機(jī)制來定制需要的URL格式,本文就來為大家介紹一下thinkphp中設(shè)置url格式的方法。
ThinkPHP 作為 PHP 框架,是單一入口的,那么其原始的 URL 便不是那么友好。但 ThinkPHP 提供了各種機(jī)制來定制需要的 URL 格式,配合 Apache .htaccess 文件,更是可以定制出人性化的更利于 SEO 的 URL 地址來。
.htaccess文件是 Apache 服務(wù)器中的一個配置文件,它負(fù)責(zé)相關(guān)目錄下的網(wǎng)頁配置。我們可以利用 .htaccess 文件的 Rewrite 規(guī)則來隱藏掉 ThinkPHP URL 中的 index.php 文件(即入口文件),這也是 ThinkPHP URL 偽靜態(tài)的第一步。
例如原來的 URL 為:
http://127.0.0.1/index.php/Index/insert
去掉 index.php 之后變?yōu)椋?/p>
http://127.0.0.1/Index/insert
如此一來,就變成了 http://服務(wù)器地址/應(yīng)用模塊名稱/操作名稱[/變量參數(shù)] 的常見 URL 格式。
更改 Apache httpd.conf 配置文件
提示:如果在虛擬主機(jī)商配置,請直接配置第三、四步,因為支持 .htaccess 的空間已經(jīng)配置好了前面兩步。
用編輯器打開 Apache 配置文件 httpd.conf(該文件位于 Apache 安裝目錄Apache2conf),并按如下步驟修改。
一、加載了 mod_rewrite.so
確認(rèn)加載了 mod_rewrite.so 模塊(將如下配置前的 # 號去掉):
LoadModule rewrite_module modules/mod_rewrite.so
二、更改 AllowOverride 配置
更改需要讀取 .htaccess 文件的目錄,將原來的目錄注釋掉:
#<Directory "C:/Program Files/Apache Group/Apache2/htdocs"> <Directory E:/html/myapp>
更改 AllowOverride None 為 AllowOverride FileInfo Options ,更改后的配置如下所示:
#<Directory "C:/Program Files/Apache Group/Apache2/htdocs"> <Directory E:/html/myapp> AllowOverride FileInfo Options
.htaccess 是基于目錄來控制的,
三、添加 .htaccess 文件 Rewrite 規(guī)則
在需要隱藏 index.php 的目錄下(本教程中為 E:/html/myapp,也即入口文件所在目錄)創(chuàng)建 .htaccess 文件,并寫入如下規(guī)則代碼:
<IfModule mod_rewrite.c> RewriteEngine on #不顯示index.php RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] </IfModule>
四、更改項目配置文件
編輯項目配置文件 Conf/config.php ,將 URL 模式配置為 2(Rewrite模式):
'URL_MODEL'=>2,
至此,各個配置已經(jīng)完成。保存各配置文件后,重啟 Apache 服務(wù)器并刪除 Runtime 目錄下的項目緩存文件,在瀏覽器訪問隱藏 index.php 后的地址測試是否成功:
http://127.0.0.1/html/myapp/Index/index
如果訪問成功,那么利用 Apache .htaccess 文件的 Rewrite 規(guī)則隱藏 index.php 入口文件的配置就成功了。
推薦教程:thinkphp教程
The above is the detailed content of How to set pseudo-static in thinkphp. 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)

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.
