2011-05-23 127 views
17

我正在使用ASP .NET rewriteModule将http://example.com重写为http://www.example.com使用rewriteModule从页面中删除.aspx?

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

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

<rewrite> 
     <rules> 
      <rule name="Canonical" stopProcessing="true"> 
       <match url=".*"/> 
       <conditions> 
        <add input="{HTTP_HOST}" pattern="^([a-z]+[.]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共享虚拟主机上。

回答

21

这些是我用每个项目开始的标准重写规则。我用所有的网页只有干净的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> 

页,我需要解析出ID(这种情况下,数只),并把它添加到查询字符串我添加了类似的规则面前:

<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页面重定向到干净的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 = “{R:1}” 与URL = “{ToLower将:{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会将它们作为两个单独的页面编入索引。

+0

你是男人! 一件事,如果你知道该怎么做,现在我需要向他们发送脚趾的URL,并将其添加的ASPX没有他们看到的,但有可能,如果他们去到.aspx它会删除在.aspx? – Danpe 2011-06-15 21:11:11

+1

已更新答案以包含第二个问题。 – Serge 2011-06-16 03:03:53

+0

我尝试在我的一个项目中使用此URL作为我的URL rewirte。去除扩展完美地崇拜。然而,我似乎正在为Ajax的弹出窗口问题。在我的莫代尔弹出延长和日历弹出延长。我确实提出了这个票。但到目前为止没有回应。 http://stackoverflow.com/questions/33243017/url-rewrite-create-compactible-issues-with-ajaxtoolkit – Lemdor 2015-10-26 19:17:27

3
<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的x.aspx部分,那么它应该适用于所有页面

+0

@stack尼斯THX重新连接到网页,但我想所有的页面看起来友好而不仅仅是Register.aspx ,例如我也有Login.aspx和Products.aspx等等。 – Danpe 2011-05-23 13:17:13

+0

@stack我和正则表达式不顺利在一起的xD 所以,如果你能告诉我一个例子,我会很高兴:) – Danpe 2011-05-23 13:23:31

+0

尝试更新的规则 - 这有一个包罗万象的,而不是硬编码的寄存器 - 这将需要通过网页进行测试排除 – stack72 2011-05-23 13:23:41

5

首先,您需要删除.aspx(默认值为)。ASPX)和重定向到默认更改浏览器的地址,然后添加在.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> 
+0

塞尔的答案是丰富的,但你的工作。多谢,伙计。 :) – Jamil 2015-08-24 10:18:02

+0

它给我follwing错误 说明:HTTP 404。您正在寻找(或它的一个依赖)可能已被删除的资源,有其名称更改,或者暂时不可用。请检查以下网址并确保它拼写正确。 请求的URL:/collections/.aspx – 2015-12-09 10:40:56