Enabling Pseudo-Static URLs in ThinkPHP5
To hide the application entry file index.php from URLs in ThinkPHP5, URL rewriting can be used. Below are configuration examples for different server environments.
Apache Configuration
Place the following code in a .htaccess file in the same directory as your index.php file. Most ThinkPHP installations already include this file by default.
<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]
</IfModule>
If the above doesn't work, modify the RewriteRule line as follows:
# Original line
RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]
# Modified alternatives
RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
OR
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
IIS Configuration
If using IIS with ISAPI_Rewrite, add the following rule to your httpd.ini file:
RewriteRule (.*)$ /index\.php\?s=$1 [I]
To newer IIS versions using web.config, insert the followinng rewrite section:
<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 Configuration
In older Nginx versions that do not support PATHINFO, use the following rewrite rule in your nginx.conf file:
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
If your application is installed in a subdirectory (e.g., youdomain), adjust the configuration acordingly:
location /youdomain/ {
if (!-e $request_filename){
rewrite ^/youdomain/(.*)$ /youdomain/index.php?s=/$1 last;
}
}
Resulting URL Format
Before configuration:
http://serverName/index.php/module/controller/action/[paramName/paramValue...]
After applying URL rewriting:
http://serverName/module/controller/action/[paramName/paramValue...]
Alternative Manual PATH_INFO Handling
If you cannot modify server configurations, you may attempt to manually set PATH_INFO in your index.php file. Note that this is not a guaranteed solution and depends on server settings:
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'];