You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询:如何将指定.htaccess重写规则转换为web.config规则

将.htaccess重写规则转换为web.config配置

先明确原.htaccess中两条规则的作用:

  • 第一条:如果请求的路径不是真实文件或目录,将请求转发到index.php,并保留原查询参数
  • 第二条:对包含/index.php的请求执行301永久重定向,去掉URL中的index.php

对应的IIS web.config配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <!-- 对应原规则:移除URL中的index.php并301重定向 -->
                <rule name="Remove index.php" stopProcessing="true">
                    <match url="^(.*)/index\.php$" />
                    <conditions>
                        <add input="{THE_REQUEST}" pattern="^[A-Z]{3,}\s(.*)/index\.php\s" />
                    </conditions>
                    <action type="Redirect" url="{R:1}" redirectType="Permanent" />
                </rule>
                
                <!-- 对应原规则:将非文件/非目录请求转发到index.php -->
                <rule name="Forward to index.php" 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?{R:1}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

规则对应说明:

  • Remove index.php规则:

    • <match>节点匹配以/index.php结尾的URL
    • <conditions>里的{THE_REQUEST}对应原.htaccess的%{THE_REQUEST},确保匹配的是实际外部请求而非内部重写的请求
    • redirectType="Permanent"对应原规则的R=301,stopProcessing="true"对应L标记(停止后续规则处理)
  • Forward to index.php规则:

    • <conditions>中的两个negate="true"对应原规则的!-f和!-d,表示排除真实存在的文件和目录
    • appendQueryString="true"对应原规则的QSA标记,自动保留原URL的查询参数
    • stopProcessing="true"对应L标记

内容的提问来源于stack exchange,提问作者ValentinaMotta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 09:39:26