【问题标题】:Web.config URL Rewrite specific match URLWeb.config URL 重写特定匹配 URL
【发布时间】:2013-06-07 20:53:25
【问题描述】:

我有以下 web.config URL 重写规则:

<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
    <match url="^propertysearch\.asp$" />
    <conditions>
        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
        <add input="{QUERY_STRING}" pattern="^urlrewrite=([^=&amp;]+)$" />
    </conditions>
    <action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
    <match url="^([^/]+)/?$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="propertysearch.asp?urlrewrite={R:1}" />
</rule>

问题在于它将它应用于所有内容,所以我想做的是更改匹配 url 语法以告诉它仅在 URL 中包含“-to-rent-in-”时运行,所以...

www.domain.com/villas-to-rent-in-florida

将符合条件并应用规则,但这不会

www.domain.com/testentry

我尝试了不同的变体,但总是出错:(

【问题讨论】:

  • -to-rent-in- 可以在 url 中的任何位置还是仅在第一个 / 之前?
  • 感谢您回来。仅在第一个 / 之前。本质上,URL 将始终如下所示:www.domain.com/[property type]-to-rent-in-[location] 所以 www.domain.com/villas-to-rent-in-florida, www.domain。 com/apartments-to-rent-in-new-york 等希望对您有所帮助:)
  • 我无法测试我在星期一之前发布的答案,所以如果它不起作用,请不要犹豫发表评论!
  • 您好,非常感谢您 :)。可悲的是,它不太好用,当我访问其中一个网址时,它会提供一个暂时找不到的页面......非常抱歉 - 非常感谢您的帮助。
  • 效果很好,感谢您为我这样做。非常感谢:)

标签: url-rewriting web-config


【解决方案1】:

要让 url 仅在 -to-rent-in- 是 url 第一部分的一部分时触发重写,您可以使用:

<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
    <match url="^(.+-to-rent-in-.+)/?$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="propertysearch.asp?urlrewrite={R:1}" />
</rule>

url="^(.+-to-rent-in-.+)/?$" 将匹配在-to-rent-in- 之前至少包含一个字符且在之后至少包含一个字符的任何网址。

您不想将之前或之后的字符限制为仅是字母数字,因为您的网址可能类似于:www.domain.com/apartments-to-rent-in-new-york(其中new-york 包含-

【讨论】:

    猜你喜欢
    • 2013-01-21
    • 2023-01-10
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 2017-08-23
    • 2012-06-04
    相关资源
    最近更新 更多