Web.config 在某些情况下重写规则,否则保留原始规则

Web.config Rewrite rule in some cases, otherwise leave original

有人可以帮我写重写规则,基本上是这样的:

如果 URL 中有“siteA”,但前提是继续使用“wp-admin、wp-content 或 wp-includes”,然后删除“siteA”。在所有其他情况下,在 URL.

中保留“siteA”

例如

domain.com/siteA/hello -> remains domain.com/siteA/hello

domain.com/siteA/wp-admin/some.css -> 重写为 domain.com/wp-admin/some.css

domain.com/siteA/wp-content/some.css -> 重写为 domain.com/wp-content/some.css

domain.com/siteA/wp-includes/some.css -> 重写为 domain.com/wp-includes/some.css

我尝试了几种选择,但我显然不擅长正则表达式和 url-重写;其中一项尝试:

        <rule name="Custom: remove subsite from resources paths">
          <match url="^siteA/(wp-(content|admin|includes).*)"/>
          <action type="Rewrite" url="{R:1}"/>
        </rule> 

也试过

            <rule name="Custom: remove subsite from resources paths">
              <match url="^siteA/(wp-(content|admin|includes))/(*)"/>
              <action type="Rewrite" url="{R:2}{R:3}"/>
            </rule>     

还有

    <rule name="Custom: remove subsite from resources paths">
      <match url="^siteA/(wp-(content|admin|includes).*)"/>
      <action type="Rewrite" url="{R:2}{R:3}"/>
    </rule> 

这是我的完整规则节点(由于多站点而由 Wordpress 生成)。

所以事情是,它生成

域名.com/siteA/wp-includes/some.css

而不是

域名.com/wp-includes/some.css

因此,当涉及到 wp-includes 等时,我需要丢失“siteA”。在其他情况下(常规 post 域。com/siteA/regularPost)应保持原样。

    <rewrite>
        <rules>
            <rule name="WordPress Rule 1" stopProcessing="true">
                <match url="^index\.php$" ignoreCase="false" />
                <action type="None" />
            </rule>
            <rule name="WordPress Rule 2" stopProcessing="true">
                <match url="^([_0-9a-zA-Z-]+/)?wp-admin$" ignoreCase="false" />
                <action type="Redirect" url="{R:1}wp-admin/" redirectType="Permanent" />
            </rule>
            <rule name="WordPress Rule 3" stopProcessing="true">
                <match url="^" ignoreCase="false" />
                <conditions logicalGrouping="MatchAny">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
                </conditions>
                <action type="None" />
            </rule>
            <rule name="WordPress Rule 4" stopProcessing="true">
                <match url="^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*)" ignoreCase="false" />
                <action type="Rewrite" url="{R:1}" />
            </rule>
            <rule name="WordPress Rule 5" stopProcessing="true">
                <match url="^([_0-9a-zA-Z-]+/)?([_0-9a-zA-Z-]+/)?(.*\.php)$" ignoreCase="false" />
                <action type="Rewrite" url="{R:2}" />
            </rule>
            <rule name="WordPress Rule 6" stopProcessing="true">
                <match url="." ignoreCase="false" />
                <action type="Rewrite" url="index.php" />
            </rule>                 
        </rules>
    </rewrite>

我不确定你想要什么。根据你的问题描述,你尝试过的几种方案基本都达到了你的目的。我测试了它,它有一些问题。所以我改进它重写siteA.

 <rule name="remove siteA">
                <match url="siteA/(wp-(content|admin|includes))/(.*)" />
                <action type="Rewrite" url="{R:1}/{R:3}" />
 </rule>

rewrite进程可以检查失败的请求跟踪。