【问题标题】:Redirect all URLs with trailing slash except one重定向所有带有斜杠的 URL,除了一个
【发布时间】:2019-07-25 21:45:03
【问题描述】:

我的 Wordpress 环境在 IIS/Azure 上运行。我有一个需要帮助的 web.config 文件。我已经成功地将所有带有斜杠的 URL 的流量永久重定向到没有斜杠的同一个 URL。效果很好,除了我需要为一条路径否定这条规则,这样我就不会破坏 Wordpress 提供的 wp-json (API) 功能。

我试了一下,但这条否定规则似乎不起作用。

<rule name="Remove trailing slash" stopProcessing="true">
  <match url="(.*)/$" ignoreCase="true" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <add input="{REQUEST_URI}" negate="true" pattern="^\wp-json$" ignoreCase="true" />
  </conditions>
  <action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>

我想要豁免的 URL 仍然重定向到非斜杠 URL,在这种情况下,我需要保留斜杠。

【问题讨论】:

    标签: wordpress web-config trailing-slash


    【解决方案1】:

    我不是正则表达式专家,但也许消极的前瞻性可能会有所帮助。不过,我的匹配组可能不正确。

    ^((?!wp-json\/).)*$
    

    (基于此处的讨论:Regular expression to match a line that doesn't contain a word

    【讨论】:

    • 我将您建议的正则表达式放入规则的相关部分,但它似乎不起作用:&lt;add input="{REQUEST_URI}" negate="true" pattern="^((?!wp-json\/).)*$" ignoreCase="true" /&gt;
    【解决方案2】:

    DJ,

    我看到了你在 UpWork 上的添加,我想我会给你一些我发现的东西。他们不会让我在提案中提交,但我认为在必要之前收取费用是不对的。

    我在 (https://moz.com/community/q/web-config-redirects-not-working-where-a-trailing-slash-is-involved) 找到了这条规则,并认为它可能是您正在寻找的。它应该同时使用斜线和不使用斜线。

    <rule name="Test" stopProcessing="true">
        <match url="^example_folder" />
        <action type="Redirect" url="/newfolder" />
    </rule>
    

    如果您仍然需要帮助,请给我留言,我很乐意为您服务。我的 upwork 个人资料是:https://www.upwork.com/fl/johns770?viewMode=1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-22
      相关资源
      最近更新 更多