我需要一些url重写的帮助。我的php站点在windows服务器上运行。我正在尝试重写urls,这样分类和文章看起来就像这样:
hxxp://domain.com/类别名称
hxxp://domain.com/文章标题
这就是我在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>发布于 2013-05-25 00:32:08
因为规则是按照它们显示的顺序触发的。因此,当你想重写文章时,你需要一些东西来避免触发第一条规则。
例如,遵循您已有的约定,它可以是:
<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}。
https://stackoverflow.com/questions/16737668
复制相似问题