【问题标题】:URL Rewrite web.configURL 重写 web.config
【发布时间】:2013-05-24 14:42:39
【问题描述】:

我在 url 重写方面需要一点帮助。我的 php 站点在 windows 服务器上运行。我正在尝试重写 url,因此类别和文章都如下所示:

hxxp://domain.com/category-name
hxxp://domain.com/article-title

这就是我在 web.config 中的内容。它适用于类别但不适用于文章,我做错了什么?

<rule name="category">
    <match url="^([_0-9a-z-]+)" />
        <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
    <action type="Rewrite" url="category.php?slug={R:1}" />
</rule>
<rule name="article">
    <match url="^([_0-9a-z-]+)" />
        <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
    <action type="Rewrite" url="article.php?slug={R:1}" />
</rule>

【问题讨论】:

    标签: url mod-rewrite url-rewriting


    【解决方案1】:

    因为规则是按照它们显示的顺序触发的。所以当你想重写文章时,你需要一些东西来避免触发第一条规则。

    例如,按照您的约定,它可能是:

    <rule name="category">
        <match url="^category-([_0-9a-z-]+)" />
            <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            </conditions>
        <action type="Rewrite" url="category.php?slug={R:1}" />
    </rule>
    <rule name="article">
        <match url="^article-([_0-9a-z-]+)" />
            <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            </conditions>
        <action type="Rewrite" url="article.php?slug={R:1}" />
    </rule>
    

    仅当路径以category- 开头时才会触发第一条规则,而仅当路径以article- 开头时才会触发第二条规则。

    请注意,使用{R:1} 作为反向引用,您将只有category-article- 之后的内容,因此如果您想保持与以前相同的行为,您可以改用{R:0}

    【讨论】:

      猜你喜欢
      • 2014-06-13
      • 1970-01-01
      • 2017-08-23
      • 2012-06-04
      • 2012-12-06
      • 2011-05-23
      • 2010-12-16
      • 2014-08-12
      • 1970-01-01
      相关资源
      最近更新 更多