Windows Server Web.config 剥离索引文件名
我正在使用以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了在重定向后删除
index.html
,请删除{R:1}
。但随后您需要修改该规则,使其仅触发 /index.html 请求,并创建一个在包含{R:1} 以便 example.com/mypage.html 的请求仍将被正确重定向。
编辑:
编辑#2
最终答案是!
根据我们的聊天对话,我认为这是最终的规则集:
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: