简体   繁体   中英

Rewrite rule that matches a folder in the base directory but not folders with the same name at deeper levels

I'm trying to add a rewrite rule for WordPress to use this Orbisius Media Protector plugin . The plugin assumes uploads are in /wp-content/ but I've moved uploads to /assets/.

I changed RewriteCond %{REQUEST_URI} ^(.*?/?)wp-content/uploads/.* [NC] to RewriteCond %{REQUEST_URI} ^(.*?/?)assets/.* [NC] but that's affecting files located at wp-content/plugin-name/assets, so I need a RegEx that only matches /assets/ at the first level of the URL.

I'm working in an IIS environment. I've used the server manager URL Rewrite functionality to import the .htaccess rules for web.config, so this is the code I'm actually working with:

<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>

I need the rewrite to apply to all files located in domain.com/assets/filename.ext but ignore files located in other folders named assets like domain.com/wp-content/plugins/plugin-name/assets/filename.ext .

I am not sure about the plugin, however if you want the patternt o match the assests folder at root level and not at any other level you can try the below regular expression ^(/?assets)/.* , this will work either as a condition or as a pattern in the match url where you have .

With the regular expression you have ^(. ?/?)assets/. this matches to assets at all directory levels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM