web.config 索引重定向
我对托管在 Windows 服务器上的项目使用标准 web.config 文件。它负责 2 项任务。
- 它将网站的非 www 版本重定向到 www 版本。
- 它将索引文件重定向到根目录。
见下文:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<rewrite>
<rules>
<rule name="CanonicalHostNameRule1" stopProcessing="true">
<match url="index\.asp(?: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>
这非常有效,除了 1 个问题。索引重定向将所有子目录重定向到主目录index。
例如:
http://www.example.com/index.asp 正在重定向到 http://www.example.com 就像它应该的那样。
但是, http://www.example.com/about/index.asp 也是重定向到 http://www.example.com。我希望它重定向到 http://www.example.com/about/
任何帮助都会不胜感激。
I use a standard web.config file for my projects that are hosted on Windows servers. It takes care of 2 tasks.
- It redirects the non-www version of the site to the www version.
- It redirects the index file to the root.
See below:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<rewrite>
<rules>
<rule name="CanonicalHostNameRule1" stopProcessing="true">
<match url="index\.asp(?: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>
This works great, except for 1 problem. The index redirect is redirecting all sub-directories to the main, index.
For example:
http://www.example.com/index.asp is redirecting to http://www.example.com like it should.
But, http://www.example.com/about/index.asp is also redirecting to http://www.example.com. I would like it to redirect to http://www.example.com/about/
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,在这里没有得到回应后,我在各个.Net论坛上发帖,终于得到了答案。这条规则将修复它:
Well, after getting no response here, I posted on various .Net forums and finally got an answer. This rule will fix it: