2011-03-30 41 views
1

我正在使用IIS7的URLRewrite功能在我的ASP.NET WebForms应用程序的URL中隐藏.aspx扩展名。如何阻止使用IIS7的URLRewrite模块直接访问.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

这保留“pr etty“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格式?

回答

0

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

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

我会用这种方法预测(与你的其他规则)的一个问题是,如果你有一个名为contact.aspx的页面和一个名为/contact的文件夹。这会产生一些意想不到的问题。

+0

谢谢,多数民众赞成我正在寻找,我以为我试过,但也许我的语法有点不对。 – 2011-04-01 11:38:20