7

我有一个.htaccess文件,它从文件中删除 .php 扩展名,例如将 so.com/question.php 转换为so.com/question重定向文件夹中的索引文件,例如so.com/answer/index .php完全按照此答案中的描述重定向到so.com/answer/

我刚刚在 IIS7 上本地设置了我的站点,需要在web.config中重新创建相同的行为,但不知道从哪里开始转换/重写 .htaccess 文件。

4

1 回答 1

31

我发现 IIS7 及更高版本可以使用URL Rewrite 模块导入 Apache .htaccess 规则。

  1. 通过Microsoft Web 平台安装程序安装URL 重写模块
  2. 启动 IIS 管理器,然后在左侧的“连接”窗格中,选择您需要的站点(例如默认网站)
  3. 在中心(功能视图)双击URL Rewrite
  4. 在右侧面板中单击Import Rules...然后将 .htaccess 文件中的规则粘贴到Rewrite rules框中
  5. 单击右栏中的应用。

在上面的特定问题中,以下 .htaccess 重定向规则

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

生成以下 web.config 文件。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Imported Rule 1" stopProcessing="true">
                    <match url="^(.*)$" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}.php" matchType="IsFile" ignoreCase="false" />
                    </conditions>
                    <action type="Rewrite" url="{R:1}.php" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
于 2012-09-17T18:39:35.640 回答