【问题标题】:How do I block direct access to .aspx pages using IIS7's URLRewrite module?如何使用 IIS7 的 URLRewrite 模块阻止对 .aspx 页面的直接访问?
【发布时间】: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


    【解决方案1】:

    以下规则将从 URL 中去除 .aspx,然后将浏览器重定向到无扩展名的 URL:

    <rule name="Redirect to friendly" stopProcessing="true">
        <match url="^(.*)\.aspx$" />
        <action type="Redirect" url="{R:1}" />
    </rule>
    

    如果您有一个名为contact.aspx 的页面和一个名为/contact 的文件夹,我会预见到这种方法(以及您的其他规则)的一个问题。这可能会产生一些意想不到的问题。

    【讨论】:

    • 谢谢,这正是我想要的,我想我已经尝试过了,但也许我的语法有点错误。
    猜你喜欢
    • 2017-09-23
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 2015-03-09
    相关资源
    最近更新 更多