【问题标题】:Removing .aspx from pages using rewriteModule?使用 rewriteModule 从页面中删除 .aspx?
【发布时间】:2011-08-31 03:42:07
【问题描述】:

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

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

然后我在&lt;system.webServer&gt;里面有这个。

    <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 上进行共享虚拟主机。

【问题讨论】:

    标签: .net web-config url-rewriting


    【解决方案1】:

    首先您需要删除 .aspx (default.aspx) 并重定向到 default 以更改浏览器地址,然后添加 .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>
    

    【讨论】:

    • Serge 的回答提供了丰富的信息,但您的工作正在发挥作用。多谢,伙计。 :)
    • 它给了我以下错误描述:HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除、更改名称或暂时不可用。请查看以下 URL 并确保其拼写正确。请求的 URL:/collections/.aspx
    【解决方案2】:

    这些是我开始每个项目时使用的标准重写规则。我只对所有页面使用干净的 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 会将它们作为两个单独的页面进行索引。

    【讨论】:

    • 你就是男人!如果您知道该怎么做,只有一件事,现在我需要将 URL 发送给他们,它会在他们不看到的情况下添加 aspx,但是如果他们转到 .aspx 是否有可能会删除 .aspx?
    • 更新了答案以包含您的第二个问题。
    • 我尝试在我的一个项目中使用它来重写我的 URL。删除扩展工作完美。但是,我似乎遇到了 Ajax 弹出窗口的问题。在我的模态弹出扩展器和日历弹出扩展器中。我确实为此提出了一张票。但到目前为止没有任何回应。 stackoverflow.com/questions/33243017/…
    【解决方案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>
    

    这样就可以了 - 我已经在我的本地机器上生成了这个 vis IIS - 将 myserver.com 更改为您自己的 URL。您可以更改正则表达式以实际处理 url 的 x.aspx 部分,然后它应该适用于所有页面

    【讨论】:

    • @stack 很好,谢谢,但我希望所有页面看起来都很友好,而不仅仅是 Register.aspx,例如我还有 Login.aspx 和 Products.aspx 等等。
    • @stack Me 和 RegEx 不能很好地结合在一起 xD 所以如果你能给我举个例子我会很高兴:)
    • 尝试更新的规则 - 这有一个捕获所有而不是硬编码的寄存器 - 这需要跨页面测试排除
    • @stack 我得到“500(内部服务器)错误”。
    • 您是否已将 myserver.com 更改为您自己的 url?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 2014-09-04
    • 1970-01-01
    相关资源
    最近更新 更多