必须安装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"></match><conditions logicalgrouping="MatchAll"><add input="{REQUEST_FILENAME}" matchtype="IsFile" negate="true"></add><add input="{REQUEST_FILENAME}" matchtype="IsDirectory" negate="true"></add></conditions><action type="Rewrite" url="index.php?s={R:1}" appendquerystring="true"></action></rule></rules></rewrite></system.webserver></configuration>
关键说明:
-
:匹配所有请求路径(等效于 Apache 的(.*)$) - 两个
:确保不重写真实存在的文件(如.js、.css、图片)和目录(如public/),避免静态资源 404 @#@#@#@#@#@#@#@#@#@0)- ThinkPHP 配置需启用 PATHINFO:确认
config/app.php中'url_model' => 1(默认就是 1,即兼容模式;若为 2,需额外适配) -
public目录应设为网站根目录(推荐做法),此时web.config应放在public/下,且无需加子目录前缀 - 若项目不在根目录(如部署在
/myapp/子目录),需在中加上路径: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 可正常解析。
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











