【问题标题】:Generic Web.Config Rewrite to remove query strings and add url seo slug通用 Web.Config 重写以删除查询字符串并添加 url seo slug
【发布时间】:2013-04-01 18:29:25
【问题描述】:

我正在尝试创建以下 URL 重写。

<rule name="Imported Rule 3">
    <match url="^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)" ignoreCase="true" />
    <action type="Rewrite" url="/{R:0}/{R:1}.aspx?p={R:2}&title={R:3}" appendQueryString="true" />                  
</rule>

我希望以下网址重写为以下内容。

/Catalog/Product/1/Title-Description

/Catalog/Product.aspx?pid=1&title=Title-Description

/Events/Detail/1/Today
/Events/Detail.aspx?pid=1&title=Title-Description

基本上,我希望写作具有通用性并适用于多种场景。 每当有一个带有 4 个斜杠的 url 时,我希望重写规则将其拾取并将其转换为 url。

所以事件的url为

1/1/1/1 将是有效的。

这将转换为

/1/1.aspx?pid=1&title=1

只是给你一个例子。

我不想为每个场景编写单独的重写。

但是,我编写的上述重写一直在服务器上引发错误,我似乎无法找出我的语法有什么问题。服务器出现了一个通用的 500 错误,所以我无法缩小范围。

我很确定问题出在操作标签上,

我写成

<action type="Rewrite" url="/{R:0}/{R:1}.aspx?p={R:2}&title={R:3}" appendQueryString="true" />     

<action type="Rewrite" url="/{R:1}/{R:2}.aspx?p={R:3}&title={R:4}" appendQueryString="true" />     

这两个选项都不起作用。

【问题讨论】:

    标签: asp.net url-rewriting seo web-config url-rewrite-module


    【解决方案1】:

    我认为有几个问题。第一个是使用 {R:0} 您已经发现自己。 {R:0} 匹配整个 URL,因为 {R:1}{R:2} 等匹配片段。所以在这种情况下你必须使用{R:1}{R:2}等。

    另一个问题是您可能错误地使用了?p={R:3} 而不是?pid={R:3}。最后,也可能是 500 错误的原因是您没有正确地 XML 编码 &amp;amp;title 中的 & 符号,应该是 &amp;amp;title

    我认为以下应该可行:

    <rule name="Imported Rule 3" stopProcessing="true">
        <match url="^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)" />
        <action type="Rewrite" url="/{R:1}/{R:2}.aspx?pid={R:3}&amp;title={R:4}" appendQueryString="false" />
    </rule>
    

    【讨论】:

    • 完美,解决了这个问题。我还必须添加一些条件来防止服务器上的实际文件被重写检测到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    • 1970-01-01
    • 2016-02-13
    • 2012-09-25
    相关资源
    最近更新 更多