spyshadow
Lv.1 等级
0 积分
楼主

API接口在IIS下怎么开放?怎么设置

刚刚安装好CMS,我的环境是IIS,伪静态这些都配置好了,API接口就是访问不通,麻烦帮我看看怎么回事?
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<!-- PHP 处理器 -->
<handlers>
<remove name="WebServiceHandlerFactory-ISAPI-4.0_32bit" />
<remove name="SimpleHandlerFactory-ISAPI-4.0_32bit" />
<remove name="PageHandlerFactory-ISAPI-4.0_32bit" />
<remove name="WebServiceHandlerFactory-Integrated-4.0" />
<remove name="SimpleHandlerFactory-Integrated-4.0" />
<remove name="PageHandlerFactory-Integrated-4.0" />
<remove name="WebServiceHandlerFactory-ISAPI-4.0_64bit" />
<remove name="SimpleHandlerFactory-ISAPI-4.0_64bit" />
<remove name="PageHandlerFactory-ISAPI-4.0_64bit" />
<remove name="ASPClassic" />
<add name="php_74" path="*.php" verb="*" modules="FastCgiModule" scriptProcessor="D:\BtSoft\php\74\php-cgi.exe" resourceType="File" requireAccess="Script" />
</handlers>

<!-- URL 重写规则 -->
<rewrite>
<rules>
<!-- 规则1:禁止访问缓存目录 -->
<rule name="DenyCache" stopProcessing="true">
<match url="^sd_content/cache/.*" />
<action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Access denied" />
</rule>

<!-- 规则2:API 重写(修改点:使用 {R:1} 传递路径参数) -->
<rule name="ApiRewrite" stopProcessing="true">
<match url="^api/(.*)$" />
<action type="Rewrite" url="api.php/{R:1}" appendQueryString="true" />
</rule>

<!-- 规则3:前端控制器 -->
<rule name="IndexFallback" stopProcessing="false">
<match url="^(.*)$" />
<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" appendQueryString="true" />
</rule>
</rules>
</rewrite>

<!-- 关闭详细错误信息 -->
<httpErrors errorMode="Custom" />
</system.webServer>
</configuration>
共 2 条回复
admin
Lv.0 等级
0 积分
系统管理员 官方开发者
你好,IIS 环境下 API 访问不通,主要看两点:URL Rewrite 是否正确安装,以及 `/api/...` 是否正确重写到 `api.php`。

你现在这条规则里:

<action type="Rewrite" url="api.php/{R:1}" appendQueryString="true" />

建议改成:

<action type="Rewrite" url="api.php" appendQueryString="true" />

Sndow CMS 的 API 入口是根目录下的 `api.php`,系统会根据 `REQUEST_URI` 解析 `/api/posts`、`/api/home` 这类接口路径,不需要把 `{R:1}` 拼到 `api.php` 后面。

可以参考下面这份 IIS web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ApiRewrite" stopProcessing="true">
<match url="^api/(.*)$" />
<action type="Rewrite" url="api.php" appendQueryString="true" />
</rule>

<rule name="IndexFallback" stopProcessing="true">
<match url="^(.*)$" />
<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" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

改完后可以先测试这几个地址:

/api/ping
/api/site_info
/api/posts

如果 `/api.php?action=ping` 能访问,但 `/api/ping` 不能访问,那就是 IIS URL Rewrite 规则没有生效;如果两个都不能访问,再检查 PHP FastCGI、站点根目录和文件权限。

另外 IIS 需要确认已经安装 “URL Rewrite Module”,否则 web.config 里的 rewrite 规则不会生效。
spyshadow
Lv.1 等级
0 积分
楼主
感谢老大,在大佬的指点下,已经完全可以了,贴一下最终的IIS配置文件代码:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<!-- PHP 处理器 -->
<handlers>
<remove name="WebServiceHandlerFactory-ISAPI-4.0_32bit" />
<remove name="SimpleHandlerFactory-ISAPI-4.0_32bit" />
<remove name="PageHandlerFactory-ISAPI-4.0_32bit" />
<remove name="WebServiceHandlerFactory-Integrated-4.0" />
<remove name="SimpleHandlerFactory-Integrated-4.0" />
<remove name="PageHandlerFactory-Integrated-4.0" />
<remove name="WebServiceHandlerFactory-ISAPI-4.0_64bit" />
<remove name="SimpleHandlerFactory-ISAPI-4.0_64bit" />
<remove name="PageHandlerFactory-ISAPI-4.0_64bit" />
<remove name="ASPClassic" />
<add name="php_74" path="*.php" verb="*" modules="FastCgiModule" scriptProcessor="D:\BtSoft\php\74\php-cgi.exe" resourceType="File" requireAccess="Script" />
</handlers>

<!-- URL 重写规则 -->
<rewrite>
<rules>
<!-- 规则1:禁止访问缓存目录 -->
<rule name="DenyCache" stopProcessing="true">
<match url="^sd_content/cache/.*" />
<action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Access denied" />
</rule>

<!-- API 接口重写 -->
<rule name="ApiRewrite" stopProcessing="true">
<match url="^api/(.*)$" />
<action type="Rewrite" url="api.php" appendQueryString="true" />
</rule>

<!-- 规则3:前端控制器 -->
<rule name="IndexFallback" stopProcessing="true">
<match url="^(.*)$" />
<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" appendQueryString="true" />
</rule>
</rules>
</rewrite>

<!-- 关闭详细错误信息 -->
<httpErrors errorMode="Custom" />
</system.webServer>
</configuration>
发表回复
👍 赞一个 👏 感谢分享 🛠️ 遇到问题了 🚀 坐等更新
😀😃😄😁😆😅😂🤣😊😇🙂🙃😉😌😍🥰😘😗😙😚😋😛😝😜🤪🤨🧐🤓😎🤩🥳😏😒😞😔😟😕🙁☹️😣😖😫😩🥺😢😭😤😠😡🤬🤯😳🥵🥶😱😨😰😥😓🤗🤔🤭🤫🤥😶😐😑😬🙄😯😦😧😮😲🥱😴🤤😪😵🤐🥴🤢🤮🤧😷🤒🤕🤑🤠
本周热门讨论