iis7url重写(删除.aspx),并得到错误404

我有一个与IIS7的URL重写模块的问题,当我添加以下规则我得到一个404错误在所有页面上。

<rule name="Remove .aspx" stopProcessing="true"> <match url="(.+)\.aspx" /> <action type="Redirect" redirectType="Permanent" url="{R:1}" /> 

我只想删除所有文件扩展名。 我迷失了,也许有人知道这个解决scheme?

提前致谢。

问候,eimeim

通过将用户redirect到不带.aspx的URL,从URL中删除.aspx只是解决scheme的一部分。

如果你做了redirect,如果URL不包含正确的名称(不带.asp扩展名),服务器如何知道执行哪个脚本(带.aspx扩展名)? 所以你必须添加一个规则来解决这个问题。 做到这一点的方法是制定一个匹配任何URL的规则,但不匹配任何现有的文件或目录。 如果我们遇到这种情况,我们可以假设它可能是一个重写的ASPX页面的链接,所以我们重写URL来添加.aspx。 如果它不是一个ASPX页面,它将导致404无论如何。

为了使这一切都与您现有的代码工作,你必须出站重写您的网页的响应,以删除所有现有的.aspx页面引用不包括扩展名。 否则,你会得到很多不必要的redirect,并形成post到aspx页面将不再工作。

最后但并非最不重要的是你必须禁用压缩,因为出站重写不能在压缩打开的情况下工作。

这一切都为您的web.config导致以下重写规则:

 <system.webServer> <rewrite> <rules> <rule name="Remove .aspx from URL" stopProcessing="true"> <match url="(.*)\.aspx$" /> <action type="Redirect" url="/{R:1}" /> </rule> <rule name="Add .aspx for non-existing files or directories"> <match url="(.*)" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="/{R:0}.aspx" /> </rule> </rules> <outboundRules> <rule name="Remove .aspx from links in the response" preCondition="Only for HTML"> <match filterByTags="A, Area, Base, Form, Frame, IFrame, Link, Script" pattern="(.*)\.aspx(\?.*)?$" /> <action type="Rewrite" value="{R:1}{R:2}" /> </rule> <preConditions> <preCondition name="Only for HTML"> <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" /> </preCondition> </preConditions> </outboundRules> </rewrite> <urlCompression doStaticCompression="false" doDynamicCompression="false" /> </system.webServer>