咨询:如何将指定.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
相关产品推荐
相关产品推荐

