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.

php Courses
php Courses
php Courses
Configuring URL Rewrite for ThinkPHP 5.1 on Apache, IIS, and Nginx

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

<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>

IIS (ISAPI_Rewrite) RewriteRule (.*)$ /index\.php\?s=$1 [I] IIS (web.config)

<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>

Nginx (low version, no native PATH_INFO support)

location / {
  // … omitted part of the original configuration
  if (!-e $request_filename) {
    rewrite ^(.*)$ /index.php?s=/$1 last;
  }
}

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'];
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.

IISApacheURL Rewrite
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.