Enabling PathInfo Mode for ThinkPHP on Nginx: Configuration and Rewrite Solutions

This guide explains why Nginx may not support ThinkPHP's PathInfo mode, and provides two detailed solutions—enabling PathInfo in the fastcgi configuration and using rewrite rules to hide index.php—along with the exact code snippets needed to apply them.

php Courses
php Courses
php Courses
Enabling PathInfo Mode for ThinkPHP on Nginx: Configuration and Rewrite Solutions

When running a ThinkPHP project on Apache, the server natively supports PathInfo mode, so all four ThinkPHP access modes work without issue. However, on Nginx this often fails because older versions lack PathInfo support and newer versions (≈0.7+) have it disabled by default.

Reason: Low‑version Nginx does not support PathInfo; higher‑version Nginx supports it but the feature is not turned on.

Solution 1 – Enable PathInfo in Nginx: In the site configuration locate the location ~ \.php$ { ... } block, remove the trailing $ symbol, and add the following directives inside the braces:

fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

Save the file and restart Nginx. The server will now support PathInfo, allowing URLs such as www.example.com/index.php/admin/index/test to work.

Note: If your templates use the U() function, replace the fastcgi_split_path_info line with: fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; Solution 2 – Rewrite to hide the entry file (index.php): In the location / { ... } block, add the following rewrite rules:

if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}

Save and restart Nginx. After this, Nginx will fully support PathInfo mode, and URLs like www.example.com/admin/index/test will resolve correctly.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPNginxServer ConfigurationThinkPHPpathinfo
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.