【发布时间】:2011-03-30 21:14:02
【问题描述】:
我正在使用 IIS7 的 URLRewrite 功能在我的 ASP.NET WebForms 应用程序的 URL 中隐藏 .aspx 扩展。
我正在使用以下配置:
<rule name="WebFormsToMVC" stopProcessing="true">
<match url="^(.*?)\.aspx\?*?.*$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}" />
</rule>`
我现在可以浏览到:
http://www.mytest.com/contact
这被重写为:
http://www.mytest.com/contact.aspx
这会在浏览器地址栏中保留“漂亮”的 url。我还更新了网站上的所有链接以使用无扩展名 URL。
问题是底层的.aspx 页面仍然可以直接访问,我想阻止这种情况。
如果用户浏览到http://www.mytest.com/contact.aspx,我希望它重定向/重写到http://www.mytest.com/contact,或者至少只返回“找不到页面”。
更新:
我设法通过将所有 .aspx 页面重定向到主目录来完成这项工作。这并不理想,因为我更愿意将它们发送到非.aspx 版本,但现在可以了。
<rule name="Block .aspx" stopProcessing="true">
<match url=".aspx" />
<action type="Redirect" url="/" />
</rule>`
如何将直接寻址 .aspx 页面的 URL 重写并重定向为我的友好 URL 格式?
【问题讨论】:
标签: asp.net iis-7 url-rewriting web-config