必须安装URL Rewrite Module并配置web.config规则,其中捕获路径、排除真实文件和目录、传参,同时ThinkPHP需启用url_model=1或2。

在 IIS 10 下为 ThinkPHP 5.1 配置伪静态,不能直接用 Apache 的 .htaccess 里的 RewriteRule 语法(如 (.*)$ /index.php?s=$1),因为 IIS 使用的是 URL Rewrite 模块,配置文件是 web.config,规则写法完全不同。
你需要在项目根目录(与 index.php 同级)新建或修改 web.config 文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ThinkPHP_Rewrite" enabled="true" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?s={R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>关键说明:
-
<match url="^(.*)$"></match>:匹配所有请求路径(等效于 Apache 的(.*)$) - 两个
<conditions></conditions>:确保不重写真实存在的文件(如.js、.css、图片)和目录(如public/),避免静态资源 404 -
<action url="index.php?s={R:1}"></action>:{R:1}是正则第一个捕获组,即原始 URL 路径,传给s参数(ThinkPHP 的 PATHINFO 入口模式) -
appendQueryString="true":保留原有查询参数(如?id=123),确保$_GET正常工作
注意事项:
- 确保 IIS 已安装 URL Rewrite Module(可从 IIS 官网下载)
- ThinkPHP 配置需启用 PATHINFO:确认
config/app.php中'url_model' => 1(默认就是 1,即兼容模式;若为 2,需额外适配) -
public目录应设为网站根目录(推荐做法),此时web.config应放在public/下,且<action url="index.php?s={R:1}"></action>无需加子目录前缀 - 若项目不在根目录(如部署在
/myapp/子目录),需在<action></action>中加上路径:url="myapp/index.php?s={R:1}",并确保app.php中'root_domain' => true或正确配置app.url和app.base_url
这样配置后,访问 http://example.com/user/list 就会正确路由到 index.php?s=/user/list,ThinkPHP 可正常解析。



















