为什么我的IIS 7重写规则没有';不起作用


Why my IIS 7 rewrite rule doesn't work?

我已经将Linux下的Wordpress Mu博客迁移到IIS 7下的WP 3,因此我需要重定向http://mydomain.com/blog/blahblah/到http://mydomain.com/blahblah/

我添加到wordpress web.config

        <rule name="rewrite /blog/">
            <match url="^/blog/([_0-9a-z-]+)/" />
            <conditions>                
            </conditions>
            <action type="Rewrite" url="/{R:1}/" />
        </rule>

因此web.config现在包含:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
                <rule name="wordpress" patternSyntax="Wildcard">
                    <match url="*" />
                        <conditions>
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        </conditions>
                    <action type="Rewrite" url="index.php" />
                </rule>
        <rule name="rewrite /blog/">
            <match url="^/blog/([_0-9a-z-]+)/" />
            <conditions>                
            </conditions>
            <action type="Rewrite" url="/{R:1}/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

Wordpress继续工作,但/blog/未被重定向。为什么?

1)最好将此规则移动到通配符规则之上

2) 你有前导斜杠^/blog/...——你不需要它:^blog/...

3) 因为没有使用任何条件,我也删除了<conditions></conditions>

<rule name="rewrite /blog/">
    <match url="^blog/([_0-9a-zA-Z'-]+)/$"/>
    <action type="Rewrite" url="/{R:1}/"/>
</rule>

上面是重写规则——您在浏览器中看到相同的url,但它后面的执行方式不同。

如果您想要重定向,请使用以下方法:

<rule name="rewrite /blog/">
    <match url="^blog/([_0-9a-zA-Z'-]+)/$"/>
    <action type="Redirect" url="/{R:1}/" redirectType="Permanent" />
</rule>
相关文章: