Windows Server Web.config 剥离索引文件名

发布于 2024-10-03 11:28:16 字数 1035 浏览 3 评论 0原文

我正在使用以下 web.config 文件将站点的非 www 版本重定向到 www 版本。但是,我还想让它也删除索引文件的文件名。

例如: 将 www.example.com/index.html 重定向到 www.example.com

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true" />
        <rewrite>
            <rules>
                <rule name="CanonicalHostNameRule" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://www.example.com/{R:1}" />
                </rule>
            </rules>
        </rewrite>
  </system.webServer>
</configuration>

编辑:

这是我更新的配置文件。但是,现在它会导致 500 错误。

请参阅下面 CodingGorilla 的答案:)

I am using the following web.config file to redirect the non-www version of a site to the www version. However, I would also like to have it strip the file name of the index file as well.

For example:
redirecting www.example.com/index.html to www.example.com

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true" />
        <rewrite>
            <rules>
                <rule name="CanonicalHostNameRule" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://www.example.com/{R:1}" />
                </rule>
            </rules>
        </rewrite>
  </system.webServer>
</configuration>

Edit:

Here is my updated config file. But, its causing a 500 error, now.

See CodingGorilla's answer below :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷乱花海 2024-10-10 11:28:16

为了在重定向后删除 index.html,请删除 {R:1}。但随后您需要修改该规则,使其触发 /index.html 请求,并创建一个在包含 {R:1} 以便 example.com/mypage.html 的请求仍将被正确重定向。

编辑:

编辑#2

最终答案是!

根据我们的聊天对话,我认为这是最终的规则集:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true" />
        <rewrite>
            <rules>
                <rule name="CanonicalHostNameRule1" stopProcessing="true">
                    <match url="index\.htm(?:l)?" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="example\.com$" />
                        </conditions>
                        <action type="Redirect" url="http://www.example.com/" />
                </rule>
                <rule name="CanonicalHostNameRule2" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^example\.com$" />
                    </conditions>
                    <action type="Redirect" url="http://www.example.com/{R:1}" />
                </rule>
            </rules>
        </rewrite>
  </system.webServer>
</configuration>

In order to get rid of the index.html after the redirect, drop the {R:1}. But then you will need to modify that rule so that it triggers only for /index.html requests and create a new rule that triggers on other pages that includes the {R:1} so that requests for example.com/mypage.html will still get redirected properly.

Edit:

Edit #2

And the final answer is!

Based on our chat conversation, I think this is the final rule set:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true" />
        <rewrite>
            <rules>
                <rule name="CanonicalHostNameRule1" stopProcessing="true">
                    <match url="index\.htm(?:l)?" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="example\.com$" />
                        </conditions>
                        <action type="Redirect" url="http://www.example.com/" />
                </rule>
                <rule name="CanonicalHostNameRule2" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^example\.com$" />
                    </conditions>
                    <action type="Redirect" url="http://www.example.com/{R:1}" />
                </rule>
            </rules>
        </rewrite>
  </system.webServer>
</configuration>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文