【问题标题】:Rewrite rule that matches a folder in the base directory but not folders with the same name at deeper levels重写匹配基目录中的文件夹但不匹配更深层次的同名文件夹的规则
【发布时间】:2019-09-16 06:07:10
【问题描述】:

我正在尝试为 WordPress 添加重写规则以使用此 Orbisius Media Protector plugin。该插件假定上传位于 /wp-content/,但我已将上传移至 /assets/。

我将 RewriteCond %{REQUEST_URI} ^(.*?/?)wp-content/uploads/.* [NC] 更改为 RewriteCond %{REQUEST_URI} ^(.*?/?)assets/.* [NC] 但这会影响位于 wp-content/plugin-name/assets 的文件,因此我需要一个仅匹配 URL 第一级的 /assets/ 的正则表达式。

我在 IIS 环境中工作。我已经使用服务器管理器 URL 重写功能来导入 web.config 的 .htaccess 规则,所以这是我实际使用的代码:

<rule name="Media Filtering Rule" stopProcessing="true">
  <match url="." ignoreCase="false" />
  <conditions logicalGrouping="MatchAll">
    <add input="{URL}" pattern="^(.*?/?)assets/.*" />
    <add input="{URL}" pattern="orbisius_media_protector" negate="true" />
  </conditions>
  <action type="Rewrite" url="{C:0}/?orbisius_media_protector={REQUEST_URI}" appendQueryString="true" />
</rule>

我需要重写以应用于位于 domain.com/assets/filename.ext 中的所有文件,但忽略位于名为 assets 的其他文件夹中的文件,例如 domain.com/wp-content/plugins/plugin-name/assets/filename.ext

【问题讨论】:

    标签: regex wordpress iis url-rewriting


    【解决方案1】:

    我不确定该插件,但是如果您希望模式在根级别而不是在任何其他级别匹配 assests 文件夹,您可以尝试以下正则表达式 ^(/?assets)/.* ,这将作为条件或模式在您拥有的匹配 url 中起作用。

    使用正则表达式,您有 ^(.?/?)assets/. 这匹配所有目录级别的资产。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多