IIS 7.0 Rewrite rule for catching dynamic folders with exceptions
-
13-11-2019 - |
Question
I'm trying to create a rewrite rule which catches all www.domain.com/dynamicfolder/ (should work both with and without dash) and rewrites it to /index.php?folder=dynamicfolder and also allows for additional parameters.
The thing is, there should be some exceptions, where the rewrite rule shouldn't apply, where the request actually should open/look in a real folder. Some reserved folders, you could call it. Examples of folders where the rule should be ignored: "images", "includes" and "admin".
I have a rewrite rule now, that kind of does the job, but it's only for dynamic folder names with exactly 2 characters. The new rule shouldn't have any limitations, and also - to be demanding, it should be possible to leave out a dynamic folder name, and have the rest of the rewrite url work. :-)
<rule name="Langrewrite" enabled="true">
<match url="^([a-z]{2,2})/([_0-9a-zA-Z]+)\.html" />
<conditions logicalGrouping="MatchAll" />
<action type="Rewrite" url="/index.php?navItem={R:2}&lang={R:1}" />
</rule>
Solution
Your question is a bit vague in places, but will try to guess your requirement:
<rule name="DynamicFolder" stopProcessing="true">
<match url="^(?!(?:images|includes|admin))(.+?)/" />
<action type="Rewrite" url="/index.php?folder={R:1}" />
</rule>
OTHER TIPS
It might be exactly what Tomek said, but this is how it ended up:
<rule name="Dynamic Folders" stopProcessing="true">
<match url="^(?!errorPages/|files/|gfx/)([a-zA-Z-]*)/?([a-zA-Z-]*)/([_0-9a-zA-Z]*)(\.html|\.htm)?" />
<action type="Rewrite" url="/index.php?var1={R:1}&var2={R:2}&var3={R:3}&var4={R:4}" />
</rule>