使用rewriteModule从页面中删除.aspx?

我正在使用ASP .NET rewriteModule将http://example.com重写为http://www.example.com 。

<section name="rewriteModule" type="RewriteModule.RewriteModuleSectionHandler, RewriteModule"/> 

然后我有这个<system.webServer>里面。

  <rewrite> <rules> <rule name="Canonical" stopProcessing="true"> <match url=".*"/> <conditions> <add input="{HTTP_HOST}" pattern="^([az]+[.]com)$"/> </conditions> <action type="Redirect" url="http://www.{C:0}/{R:0}" redirectType="Permanent"/> </rule> </rules> </rewrite> 

现在我想在我的页面结束时删除所有的.aspx文件。 例:

http://www.example.com/Register.aspx

会变成:

http://www.example.com/Register/

我怎样才能做到这一点?

我在使用IIS7的GoDaddy共享虚拟主机上。

这些是我开始每个项目的标准重写规则。 我只对所有页面使用干净的URL(示例第一条规则适用于www.example.com/about和第二条规则www.example.com/product/123)

 <rewrite> <rules> <rule name="Rewrite default to aspx" stopProcessing="true"> <match url="^$" ignoreCase="false" /> <action type="Rewrite" url="default.aspx" /> </rule> <rule name="Rewrite page to aspx" stopProcessing="true"> <match url="^([a-z0-9/]+)$" ignoreCase="false" /> <action type="Rewrite" url="{R:1}.aspx" /> </rule> </rules> </rewrite> 

我需要parsing的ID(这个案例号只)的页面,并将其添加到查询string我添加到前面的类似的规则:

 <rule name="Rewrite Product ID" stopProcessing="true"> <match url="^product/([0-9]+)$" ignoreCase="false"/> <action type="Rewrite" url="product.aspx?id={R:1}"/> </rule> 

如果要在URL中使用小写字母和大写字母,请将ignoreCase =“true”

编辑回答你的第二个问题加上奖金

这条规则将把aspx页面redirect到干净的URL:

 <rule name="Redirect to clean URL" stopProcessing="true"> <match url="^([a-z0-9/]+).aspx$" ignoreCase="true"/> <action type="Redirect" url="{R:1}"/> </rule> 

用url =“{ToLower:{R:1}}”replaceurl =“{R:1}”,将URL更改为小写。 看下面为什么你会想这样做。

另外一个好主意,更新表单操作,以便邮政不会返回到丑陋的URL。 使用IIS 7.5或更新的这应该工作:

  if (!String.IsNullOrEmpty(Request.RawUrl)) form1.Action = Request.RawUrl; 

或为IIS 7:

  if (!String.IsNullOrEmpty(Context.Request.ServerVariables["HTTP_X_ORIGINAL_URL"])) form1.Action = Context.Request.ServerVariables["HTTP_X_ORIGINAL_URL"]; 

还有一件事要记住…这是一个好主意,保持所有的URL小写。 在URL中混合大小写字符会为SEO / Google带来重复的内容问题。 例如,website.com/About和website.com/about会加载相同的页面,但是Google会将它们作为两个单独的页面进行索引。

首先,您需要删除.aspx( default.aspx )并redirect到默认值以更改浏览器地址,然后添加.aspx并重新连接到使用IIS的页面

 <rewrite> <rules> <clear /> <rule name="Redirect to clean URL" enabled="true" stopProcessing="true"> <match url="^([a-z0-9/]+).aspx$" ignoreCase="true" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false" /> <action type="Redirect" url="{R:1}" /> </rule> <rule name="RewriteASPX" enabled="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="{R:1}.aspx" /> </rule> </rules> </rewrite> 
 <rewrite> <rules> <remove name="RewriteUserFriendlyURL1" /> <remove name="RedirectUserFriendlyURL1" /> <rule name="RedirectUserFriendlyURL2" stopProcessing="true"> <match url="^www\.myserver\.com/(.*)\.aspx$" /> <conditions> <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" /> </conditions> <action type="Redirect" url="www.myserver.com/{R:1}" appendQueryString="false" /> </rule> <rule name="RewriteUserFriendlyURL2" stopProcessing="true"> <match url="^www\.myserver\.com/(.*)$" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="www.myserver.com/{R:1}.aspx" /> </rule> </rules> <outboundRules> <remove name="OutboundRewriteUserFriendlyURL1" /> <rule name="OutboundRewriteUserFriendlyURL2" preCondition="ResponseIsHtml1"> <match filterByTags="A, Form, Img" pattern="^(.*)www\.myserver\.com/(.*)\.aspx$" /> <action type="Rewrite" value="www.myserver.com/{R:1}" /> </rule> </outboundRules> </rewrite> 

这将做到这一点 – 我已经在我的本地机器上生成这个可见的IIS – 将myserver.com更改为您自己的URL。 你可以改变正则expression式实际上照顾的URL的x.aspx部分,那么它应该跨所有页面