Configuring URL Rewrite for ThinkPHP 5.1 on Apache, IIS, and Nginx
This guide explains how to hide the index.php entry file of a ThinkPHP 5.1 application by configuring URL‑rewrite rules for Apache, IIS (including ISAPI_Rewrite and web.config), and Nginx, and also shows a fallback PHP modification when server changes are not possible.
ThinkPHP 5.1 can hide its entry file index.php (or another entry file) using URL‑rewrite; the following server‑specific configurations illustrate how to achieve this.
Apache
<code><IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule></code>IIS (ISAPI_Rewrite)
<code>RewriteRule (.*)$ /index\.php\?s=$1 [I]</code>IIS (web.config)
<code><rewrite>
<rules>
<rule name="OrgPage" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(.*)$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:1}" />
</rule>
</rules>
</rewrite></code>Nginx (low version, no native PATH_INFO support)
<code>location / {
// … omitted part of the original configuration
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}</code>If the application is installed in a sub‑directory, adjust the Nginx pseudo‑static rule accordingly, using the directory name (e.g., youdomain ) in the rewrite target.
Original URL format: http://serverName/index.php/模块/控制器/操作/[参数名/参数值...]
After rewrite, the URL becomes: http://serverName/模块/控制器/操作/[参数名/参数值...]
When server configuration cannot be changed, a temporary PHP fix can be applied in index.php (though not recommended): $_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'];
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.