Backend Development 4 min read

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

<code>&lt;IfModule mod_rewrite.c&gt;
  Options +FollowSymlinks -Multiviews
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
&lt;/IfModule&gt;</code>

IIS (ISAPI_Rewrite)

<code>RewriteRule (.*)$ /index\.php\?s=$1 [I]</code>

IIS (web.config)

<code>&lt;rewrite&gt;
  &lt;rules&gt;
    &lt;rule name="OrgPage" stopProcessing="true"&gt;
      &lt;match url="^(.*)$" /&gt;
      &lt;conditions logicalGrouping="MatchAll"&gt;
        &lt;add input="{HTTP_HOST}" pattern="^(.*)$" /&gt;
        &lt;add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /&gt;
        &lt;add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /&gt;
      &lt;/conditions&gt;
      &lt;action type="Rewrite" url="index.php/{R:1}" /&gt;
    &lt;/rule&gt;
  &lt;/rules&gt;
&lt;/rewrite&gt;</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'];

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

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